How to install GNU C Compiler to include I/O header files

Asked by Julianloui on 2012-10-30

When I try to compile a simple GNU C source file, the compiler cannot find the iostream.h header file placed at the beginning of the file. I feel that I have not installed the GNU C Compiler properly.

#include <iostream.h>

main ( )
{
 printf(Hello");
}

Julianloui

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Solved by:
Warren Hill
Solved:
2012-10-31
Last query:
2012-10-31
Last reply:
2012-10-30
Best Warren Hill (warren-hill) said : #1

I tried compiling your code saved as "test.cpp" with the following result
$ g++ test.cpp

test.cpp:1:22: fatal error: iostream.h: No such file or directory
compilation terminated

Then did some digging see here
http://www.linuxquestions.org/questions/programming-9/c-program-not-running-in-ubuntu-771670/
https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/153064
http://stackoverflow.com/questions/1911075/g-not-finding-iostream-h-in-ubuntu

based on these the code becomes also adding the missing " before hello.

#include<iostream>
using namespace std;

main ( )
{
 printf("Hello");
}

with this result

$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:7:16: error: ‘printf’ was not declared in this scope

However from memory printf is in <stdio.h> so the code should perhaps be

#include <stdio.h>

main ( )
{
 printf("Hello");
}

Which compiles and runs as expected.

or perhaps if you want to use <iostream>

#include <iostream>
using namespace std;

main ( )
{
 cout << "Hello";
}

Which also works

There a a number of school boy errors in this code. If its your own code that's fine, we all make mistakes - it's the way we learn. However, If this example as come from a website or book then I would stop using it, unless its supposed to be an example of bad code.

Julianloui (julianloui) said : #2

Hi Warren,

Thank you so much. You are such a great mentor. I used to write some C programs before I retired some 17 years ago as an electrical engineer in Mass.

(1) I have tried out your two corrections. I can compile the one using the stdio.h header but not the one using iostream for some reason. My example with the iostream header without the "using namespace std;" line was copied from the GNU C Compiler documentation. I am attaching two iostream-related error messages. Where do I find the output (executable) file after I enter "gcc Hello.cpp -o Hello"? By the way, I am using Ubuntu 12.04.

(2) Can you spare a few minutes to look at my Question # 212508 (Creating 'Keyboard Manager' Program Using Linux Command Line)? Thanks again.

Julian Loui
--------------------------------------------------
From: "Warren Hill" <email address hidden>
Sent: Tuesday, October 30, 2012 3:01 AM
To: <email address hidden>
Subject: Re: [Question #212711]: How to install GNU C Compiler to include
I/Oheader files

> Your question #212711 on gcc-defaults in Ubuntu changed:
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/212711
>
> Status: Open => Answered
>
> Warren Hill proposed the following answer:
> I tried compiling your code saved as "test.cpp" with the following result
> $ g++ test.cpp
>
> test.cpp:1:22: fatal error: iostream.h: No such file or directory
> compilation terminated
>
>
> Then did some digging see here
> http://www.linuxquestions.org/questions/programming-9/c-program-not-running-in-ubuntu-771670/
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/153064
> http://stackoverflow.com/questions/1911075/g-not-finding-iostream-h-in-ubuntu
>
> based on these the code becomes also adding the missing " before hello.
>
> #include<iostream>
> using namespace std;
>
> main ( )
> {
> printf("Hello");
> }
>
> with this result
>
> $ g++ test.cpp
> test.cpp: In function ‘int main()’:
> test.cpp:7:16: error: ‘printf’ was not declared in this scope
>
> However from memory printf is in <stdio.h> so the code should perhaps be
>
> #include <stdio.h>
>
> main ( )
> {
> printf("Hello");
> }
>
> Which compiles and runs as expected.
>
> or perhaps if you want to use <iostream>
>
> #include <iostream>
> using namespace std;
>
> main ( )
> {
> cout << "Hello";
> }
>
>
> Which also works
>
> There a a number of school boy errors in this code. If its your own
> code that's fine, we all make mistakes - it's the way we learn.
> However, If this example as come from a website or book then I would
> stop using it, unless its supposed to be an example of bad code.
>
> --
> If this answers your question, please go to the following page to let us
> know that it is solved:
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/212711/+confirm?answer_id=0
>
> If you still need help, you can reply to this email or go to the
> following page to enter your feedback:
> https://answers.launchpad.net/ubuntu/+source/gcc-defaults/+question/212711
>
> You received this question notification because you asked the question.

Julianloui (julianloui) said : #3

Thanks Warren Hill, that solved my question.

Warren Hill (warren-hill) said : #4

1. The <iostream> version should work if use
c

but not

gcc Hello.cpp -o Hello

Because it needs to the C++ libraries which gcc can link but does not by default

2. the <stdio.h> version should compile with

gcc Hello.cpp -o Hello
or
g++ Hello.cpp -o Hello

in either case the executable will be called "Hello" and will be in the same directory as the source file

to run it make sure you are in the same directory and enter

./Hello