gcc & c++ instead of g++

Axel Freyn axel-freyn@gmx.de
Thu Sep 10 08:35:00 GMT 2009

Hi Mohsen,
On Thu, Sep 10, 2009 at 11:35:54AM +0430, Mohsen Pahlevanzadeh wrote:
> Dear All,
> I have following code:
> //////////////////////////////////////////////////
> #include <stdlib.h>
> #include <stdio.h>
> #include <iostream>    -1
> using namespace std;   -2 
> int main(){
> cout << "Mohsen" << endl  ; -3
> }//end of main func
> ///////////////////////////////////////
> I pointed 3 lines.
> when i use g++ test.c my program compiled but when i  use gcc -c test.c
> i receive following error:
> test.c:3:20: error: iostream: No such file or directory
> test.c:4: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before
> ‘namespace’
> test.c: In function ‘main’:
> test.c:6: error: ‘cout’ undeclared (first use in this function)
> test.c:6: error: (Each undeclared identifier is reported only once
> test.c:6: error: for each function it appears in.)
> test.c:6: error: ‘endl’ undeclared (first use in this function

As Ian said already: 
 - when you use "gcc", the compiler determines the programming language
   from the filename. ".c" means C,
   ".cc",".cp",".cxx",".cpp",".CPP",".c++",".C" is understood as C++ -
   source (see the man-page)
 - as your file is named ".c", gcc tries to compile it as C-Code - which
   can't work for your C++ code.
 - Use "g++" instead of "gcc"
 - rename your file as "test.cc" (and add -lstdc++)
 - use "gcc -x c++ test.c" , telling the compiler explicitely to use C++



More information about the Gcc-help mailing list