Problems linking with g++

This is a discussion on Problems linking with g++ within the Linux Programming forums, part of the Platform Specific Boards category; I've been trying for over a day now to get my program to compile and link using the g++ (3.4.3) ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    28

    Question Problems linking with g++

    I've been trying for over a day now to get my program to compile and link using the g++ (3.4.3) compiler on linux (RedHat). Previously I have used the intel compiler and have had no problems. The link errors I'm getting look like this:

    Code:
    mylibrary.so: undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
    All of the undefined references are to the STL, and to operator new and operator delete.

    If I do an 'ldd mylibrary.so', it does *not* list libstdc++, which doesn't make much sense to me as I am dynamically linking to to it. (My makefile is passing "-Wl,-Bdynamic -shared" to g++ when linking.)

    Would appreciate any suggestions, as I'm all out of ideas. I've spent the day googling, but haven't found anything helpful. I've tried every combination of every command line option to g++ that seems like it should make a difference (eg static linking to libstd++ instead of dynamic linking), but nothing works.

    Thanks,
    Just
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    Perhaps I should have also mentioned that using the STL (and operator new/delete) work fine when I write a trivial hello world program. So there's nothing wrong with my gcc/STL installation or anything like that. I probably (hopefully) just have some extra flag (or some flag missing) in my makefiles. Hopefully someone knows what it is...

    Edit: Still more info - when I add -Wl,-Bdynamic -shared to the linker options, things seem to compile and link fine, but ldd on the executable or a library doesn't list libstdc++, and when running the app it seg faults immediately. When I leave out those linker options, that's when I get the undefined references I described before. Thanks in advance.
    Last edited by Just; 07-21-2006 at 01:02 AM.
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    What's the exact command line of the failing command?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I think you'll need to link to libstdC++..... -lstdc++

    edit:
    Don't know why you segfault. But if you dynamically link to stdc++ and it doesn't have LD_LIBRARY_PATH including the stdc++ stuff, you would.
    Last edited by FillYourBrain; 07-21-2006 at 02:44 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    I don't have the exact command line with me at the moment, but it's pretty standard. g++ -llib1 -llib2 file1.o file2.o...

    Quote Originally Posted by FillYourBrain
    I think you'll need to link to libstdC++..... -lstdc++

    edit:
    Don't know why you segfault. But if you dynamically link to stdc++ and it doesn't have LD_LIBRARY_PATH including the stdc++ stuff, you would.
    When using g++, I don't think you should need to explicitly link to libstdc++, but I've tried it anyway and it made no difference. The LD_LIBRARY_PATH does have the libstdc++ library in it, but it doesn't matter as ldd doesn't even list libstdc++ as one of the dependencies.
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,529
    > I don't have the exact command line with me at the moment, but it's pretty standard. g++ -llib1 -llib2 file1.o file2.o...
    Don't guess.
    Command lines like this are order sensitive. Specifying libraries first just makes them ignored since you have no symbols yet to resolve against.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    Okay, the order might be the other way around... but I've still got no idea what the problem is. There's a also a few -L/my/library/path options in there (obviously before the -l arguments) but that's it. I've experimented with adding -shared and other similar options from the gcc man page but they make no difference. Any suggestions?
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Try putting the libraries (-llib) last in the list?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,529
    What I see is lots of hand waving and general waffle about "some problem or other"
    What I don't see are actual command lines and actual error messages.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    Ok, here is the exact call to g++ (just with the library and object names changed to protect the innocent):

    Code:
    g++ -L/my/lib/path -Wl,-rpath-link /my/lib/path -Wl,-Bdynamic -o executable_name file1.o file2.o  
    -lstdc++  -lgcc -llibrary1 -llibrary2 -lpthread -llibrary3
    Errors are as described before:
    Code:
    /my/home/dir/file1.cpp:177: undefined reference to `std::basic_string<char, std::char_traits<char>, 
    std::allocator<char> >::c_str() const'
    Or:
    Code:
    /my/home/dir/liblibrary1.so: undefined reference to `std::allocator<wchar_t>::allocator()'
    All errors involve the STL and operator new/delete. Compiles file but doesn't link. To rule out a broken installation of gcc, I've now tried two machines - both generate the same errors. I've tried moving the "-lstdc++" to different parts of the command line, but it makes no difference (and I don't think should be required at all, anyway).
    Last edited by Just; 07-23-2006 at 08:03 PM.
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  11. #11
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    There has to be something very, very strange going on. I have just created a test library (with just one single line function) using a stripped down version of my g++ command line, and it works fine (and ldd shows libstdc++ as a dependency). I then use the same command line on one of my actual libraries, and libstdc++ is not a dependency. I think I'm working in the twilight zone here...
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    28
    I think I've finally, FINALLY, figured out what was wrong. When I was specifying -L/my/lib/path to g++, in that library path was, I think, a version of libstdc++ that was built using icc not gcc (as I mentioned in my OP, I was originally using icc). Deleting those files seems to have done the trick (well, except for several other problems, but google helped with those). Thanks for the help.
    "C++ is like jamming a helicopter inside a Miata and expecting some sort of improvement."
    - Drew Olbrich

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ OpenGL linking problems
    By KoshiB in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 04:25 PM
  2. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 12:47 AM
  3. Linking problems trying to compile QT application
    By Maragato in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2005, 09:08 PM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. More linker problems
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 10:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21