LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   gcc vs. g++ -> iostream lib found vs. not found (http://www.linuxquestions.org/questions/linux-general-1/gcc-vs-g-iostream-lib-found-vs-not-found-265040/)

CooManChu 12-10-2004 07:23 PM

gcc vs. g++ -> iostream lib found vs. not found
 
Hi,

I'm using C++ on my Linux machine (Redhat 9.0) and I have a question. I was wondering if someone could tell me the main differences between using gcc and g++ as my compiler. The reason I'm curious about the differences is because when I issue gcc from the command line, the link step fails due to unresolved library references.

As a test program, I coded up the standard "Hello world" program in C++:

#include <iostream>
int main()
{
std::cout << "Hello world.\n";
return 0;
}

Issuing gcc hello.cpp results in link errors. Mainly:
undefined reference to `std::cout'

Running gcc in verbose mode (gcc -v hello.cpp) shows the include lib search order:

#include <...> search starts here:
/usr/include/c++/3.2.2
/usr/include/c++/3.2.2/i386-redhat-linux
/usr/include/c++/3.2.2/backward
/usr/local/include
/usr/lib/gcc-lib/i386-redhat-linux/3.2.2/include
/usr/include
End of search list.

I definately can find iostream in /usr/include/c++/3.2.2., but it looks as if the lib is not being pulled in for the link.

Issuing g++ hello.cpp compiles and links everything just fine. The resulting a.out executable runs as expected.

Could someone explain what it is that I need to do to use gcc correctly in this case or if I really need to bother since I can use g++ (is there a preference for gcc over g++ in any cases)?

Thanks alot,

Scott Cook
Chicago, IL

CooManChu 12-11-2004 09:20 AM

Never mind...

After looking at this a little more, I think the main difference is what gcc passes to the assembly/linkage steps compared to what g++ passes to these steps.

Running each compiler from the command line in verbose mode showed the following.

g++ by default was passing link parms: -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
gcc by default was passing link parms: -lgcc -lgcc_eh -lc -lgcc -lgcc_eh

Simply adding -lstdc++ to the parm list when invoking gcc (gcc -lstdc++ hello.cpp) results in the expected a.out. Hopefully, I'll run into a more intersting/challenging problem to post in the future. : )

Scott


All times are GMT -5. The time now is 11:51 PM.