Thread: Compilation problem

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    21

    Compilation problem

    I have a very strange and elusive problem. I am compiling a multi-file program. When I use
    Code:
    gcc -o program.app file1.cpp file2.cpp
    It works, but when I use this code:
    Code:
    gcc -c file1.cpp
    gcc -c file2.cpp
    ld -o program.app file1.o file2.o
    I get this error:
    Code:
    ld: Undefined symbols:
    ___gxx_personality_v0
    dyld_stub_binding_helper
    __ZNSt8ios_base4InitC1Ev
    __ZNSt8ios_base4InitD1Ev
    __ZSt4cout<br>
    __ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PK
    It doesn't work. I've looked all over the place, but I can't find any reason that it doesn't work. Any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try using a c++ compiler to compile c++ programs

    gcc is for c
    g++ is for c++

    > ld -o program.app file1.o file2.o
    Also use the compiler to invoke the linker. There are lots of libraries and options which need to be passed to the linker which the compiler driver knows about.

    g++ -o program.app file1.o file2.o
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    That was my mistake. I typed gcc when I meant g++. I have been using g++. I know there is a way to divide compilation and linking in c++, because I've done it before. Saddly, I don't have any of those files left. I just can't remember what worked before.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So what's wrong with
    g++ -o program.app file1.o file2.o

    Sure you could type
    g++ -v -o program.app file1.o file2.o
    to find out all the options g++ passes to the linker, then do that yourself, but what would be the point?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Actually gcc and g++ compile programs exactly the same. The difference is in linking. They are simply drivers that use file extension to compile for the correct language. The ld failed because he lacked any of the appropriate libraries to link the program. But just use g++ for everything C++ to make your life easier.

  6. #6
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    When you are compiling 50 or 60 files, you can't say
    Code:
    g++ -o program.app file1.cpp file2.cpp file3.cpp file4.cpp file5.cpp file6.cpp...
    I don't feel like setting up a makefile for this project, but it would be nice if I didn't have to recompile every file every time.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >When you are compiling 50 or 60 files, you can't say

    If you would reread Salem's post, it's the equivalent of your:
    ld -o program.app file1.o file2.o

  8. #8
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    >When you are compiling 50 or 60 files, you can't say

    If you would reread Salem's post, it's the equivalent of your:
    ld -o program.app file1.o file2.o
    If you would read my post, you would see that I am compiling a large number of files, and I don't want to have to re-compile every time. It would be much easier and faster to link the old object to the new ones from the changed files than to recompile everything every time.

    I forgot to thank you, Salem, for the g++ -v tag. I'll be trying that and I'll see if it works.

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    21
    Now I feel like an idiot. -v is obviously verbose. I've used enough *nix/BSDs that I should have known that.

    It turned out that there was a whole pile of linker tags to use. For future record, here they are.
    Code:
    ld -arch ppc -dynamic -o program.app -lcrt1.o -lcrtbegin.o -L/usr/lib/gcc/darwin/3.1 -L/usr/lib/gcc/darwin -L/usr/libexec/gcc/darwin file1.o file2.o -lstdc++ -lgcc -lSystem
    Thanks alot Salem!

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > If you would read my post
    Yes, many times, but you still don't get it.

    You can pass ".o" files to g++ just as easily as .cpp files, and it will do the same thing.

    Code:
    # compile each source file separately
    g++ -c file1.cpp
    g++ -c file2.cpp
    # link the whole mess together
    g++ -o program.app file1.o file2.o
    See? object files being passed to the compiler!


    Not an ld to be seen, and no long magic lines which need to change every time you change your host or update your compiler.

    > I don't feel like setting up a makefile for this project
    Well that's just being a bone-head IMO.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You'd have thought after about three hints the light would have come on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  2. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  3. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  4. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM