Friday, June 26, 2009

Leopard and gcc

Starting from the very beginning with C++, here is that famous chestnut (filename is test.cpp):


int main () {
std::cout << "Hello, world!";
std::cout << std::endl;
return 0;
}


And immediately I'm scratching my head at weirdness on the command line in Leopard. There is a difference between gcc and g++. Why? It seems the linker is having trouble finding libstdc++. Now I have to remember to use g++.

$ g++ -o test test.cpp
$ ./test
Hello, world!
$ gcc -o test test.cpp
Undefined symbols:
...
...
"std::cout", referenced from:
__ZSt4cout$non_lazy_ptr in ccJ7ynTQ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status


Department of "WTF".

2 comments:

Guziy Alexander (Huziy Oleksandr) or just Sasha:) said...

You forgot to include
#include &lt iostream &gt

telliott99 said...

Thanks! Retesting the example, it fails even with g++, while adding "#include <iostream>" makes it work with g++ but not with gcc. Adding "using namespace std;" and with plain cout, endl, doesn't help. I haven't written anything in C++ for a long time, so will have to review what is needed, but there is clearly different behavior for g++ and gcc.