LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   C++ compilation problem in Ubuntu (https://www.linuxquestions.org/questions/linux-newbie-8/c-compilation-problem-in-ubuntu-898569/)

soumyaranjan4ever 08-21-2011 01:30 AM

C++ compilation problem in Ubuntu
 
After i create a file with text editor, when i use the compile command

gcc filename.cpp

fatal error - iostream.h: No such file or directory
compilation terminated.

And when i try
gcc -o filename.cpp
it says
gcc: no input files.

Please Help me.

Nylex 08-21-2011 01:41 AM

For C++ programs, you use g++, rather than gcc (so that the standard C++ library is linked automatically). Also, you should be using "iostream", rather than "iostream.h". The latter is the older form of the header and it probably doesn't exist on your system (it certainly doesn't on mine).

Also, the -o flag is used to specify the name of the executable, so you're not actually giving a source file to be compiled, hence the error message in the second case.

soumyaranjan4ever 08-21-2011 01:47 AM

Sir, my example file is ---

gedit name.cpp

#include<iostream>
int main()
{
cout<<"I hope this works";
}

When i am compiling with command
g++ name.cpp it gives 2 messages-- 1. In function 'int main()'
2. error - 'cout' not declared in this scope.

Nylex 08-21-2011 01:48 AM

Ah yes. You also need the line "using namespace std;" after the #include, because cout, endl and friends are in the namespace called "std" (which you'll learn about later if you get that far!).

Edit: Also, in future, please post code between [code] tags to preserve indentation.

soumyaranjan4ever 08-21-2011 01:57 AM

Thank U sir, after over a year of C programming, i was into C++ classes for over a week. i had covered much but inability to compile and test some programs was irritating. But thank U again sir, i have compiled my first C++ program. I will always be grateful to U. :) :) :)

Regards,
Soumya Ranjan (India)

Nylex 08-21-2011 01:58 AM

Please mark your thread as solved by using the "Thread Tools" at the top. Also, please write words out in full here.


All times are GMT -5. The time now is 03:57 PM.