C++ help! Linking Error?

Discussion in 'Mac Programming' started by sungyon, Jul 20, 2005.

  1. sungyon macrumors newbie

    Joined:
    Jul 20, 2005
    #1
    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:
     
  2. NuPowerbook macrumors member

    Joined:
    Jan 17, 2004
    #2
    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.
     
  3. sungyon thread starter macrumors newbie

    Joined:
    Jul 20, 2005
    #3
    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!
     
  4. macmanmatty macrumors regular

    Joined:
    Jul 10, 2005
    Location:
    I have calculated my velocity so precisely that a
    #4
    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.
     
  5. Gizmotoy macrumors 65816

    Gizmotoy

    Joined:
    Nov 6, 2003
    #5
    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)
     
  6. sungyon thread starter macrumors newbie

    Joined:
    Jul 20, 2005
    #6
    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. =)
     
  7. Gizmotoy macrumors 65816

    Gizmotoy

    Joined:
    Nov 6, 2003
    #7
    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.
     

Share This Page