Mac C++ help! Linking Error?

sungyon

macrumors newbie
Original poster
Jul 20, 2005
3
0
Hi, I am trying to run a C++ code from my terminal but keep running into problems.

For instance, when I tried to run a basic Hello World code:
gcc hello_world.cpp,
I end up with the following error message:

/usr/bin/ld: Undefined symbols:
std::ios_base::Init::Init()
std::ios_base::Init::~Init()
std::cout
std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
___gxx_personality_v0
collect2: ld returned 1 exit status

Even when I run a dummy code that does absolutely nothing, I get an error saying Undefined symbols: _main. I don't know what to do. Any suggestions? Thanks! :confused:
 

NuPowerbook

macrumors member
Jan 17, 2004
51
0
Try using g++. g++ is gcc with the C++ libraries already linked.

The command gcc by itself will not compile C++ code unless you add the libraries to the command line.
 

sungyon

macrumors newbie
Original poster
Jul 20, 2005
3
0
Thank you for your suggestion.

Well, I tried g++ instead of gcc, but it didn't quite work. When I did g++ hello_world.cpp, it returned nothing - no error but also no hello message. And when I ran my dummy code with g++, I still got the same error with ld Undefined symbols. :(

More importantly, I get the same type of error when I try to build a set of codes using a makefile. When I type "make" on the command line, it actually creates all the objects that it's supposed to create, but it fails before creating the executable file and returns the same "Undefined symbols" error:
/usr/bin/ld: Undefined symbols:
std::ios_base::Init::Init()
std::ios_base::Init::~Init()


Can you think of anything else I should try? Thanks a lot!
 

macmanmatty

macrumors regular
Jul 10, 2005
237
0
(x,y)
Are you using xcode? I remember that xcode was a real pain to setup for using the g++ complier in my programming class last semester. There is some package on the xcode cd that you have to reinstall if it is giving you a main error. Also trying putting "using namespace std;" before your main program insted of using std:: every time somtimes that causes problems.
 

Gizmotoy

macrumors 65816
Nov 6, 2003
1,086
118
sungyon said:
Thank you for your suggestion.

Well, I tried g++ instead of gcc, but it didn't quite work. When I did g++ hello_world.cpp, it returned nothing - no error but also no hello message. And when I ran my dummy code with g++, I still got the same error with ld Undefined symbols. :(
Perhaps I'm misunderstanding you here, or perhaps you misunderstand what g++ is actually doing. Executing the command g++ hello_world.cpp will not run the code you have created. All it does is link, and compile your code into something the machine can read. If you executed g++ hello_world.cpp and nothing was returned, congratulations, your program compiled successfully!

Try this, then:
g++ hello_world.cpp -o hello_world (this creates a "hello_world" executable file, if the -o is ommited, the output it put into "a.out")
./hello_world (this will run your executable, which should print the text you are expecting)
 

sungyon

macrumors newbie
Original poster
Jul 20, 2005
3
0
Oh, you are right! I am quite new to this whole programming thing, so I didn't fully understand what g++ actually does. So I did .\hello_world, and it gave me the correct message. Thanks a lot to everyone who gave me an input. =)
 

Gizmotoy

macrumors 65816
Nov 6, 2003
1,086
118
I'm glad you were are able to solve the problem. It's a little confusing at first, but nearly everything with computers seems to be that way these days... I wish you luck learning the language, and we're happy we could help you out.