Thread: Linking C++ Static Libs

  1. #1
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11

    Linking C++ Static Libs

    Could use some direction here; I have a static lib I've created using the correct tools; I know this as I have a working example test case. The only difference is the simple lib's syms are exported using extern and linked to a plain c prog. The actual use case is a c++ lib linked to a c++ program, so I've not wrapped the apis in extern braces. I've linked the calling app to the lib like this;

    Code:
    g++ -Wall -I../lib/include  -static -L../lib -lliblib  test.cc   -o test
    and I get a plethora of undefined symbols. Output of nm

    nm -guA liblib.a yields nada.

    nm liblib.a | grep Func gets me

    Code:
    0000000000000000 W _ZN10lib13Func4typeEPKc
    0000000000000000 W _ZN10lib13FuncEPKc
    0000000000000040 T _ZN10lib13Func5flushEv
    0000000000000000 W _ZN10lib13FuncEPKc
    (...)
    So the methods I'm interested are getting exported, and the name mangled polymorphic exports seem ok, but ld can't seem to get its glue on. What else should I be looking at? Should I just go ahead and wrap the methods for unmangled C export as in my test case, even though its a c++ to C++ link?

    Thanks to any and all.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If your real filename is libfoo.a, then the linker option is simply -lfoo
    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 phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    Quote Originally Posted by Salem View Post
    If your real filename is libfoo.a, then the linker option is simply -lfoo
    yeah, I know, that was a typo. If that were the actual problem the linker would say it can;t find the library, not that it couldn't find a symbol in the library no?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Are there any more typos you want to declare before anyone wastes anymore time chasing ghosts?
    You should always copy/paste your evidence.

    > If that were the actual problem the linker would say it can;t find the library, not that it couldn't find a symbol in the library no?
    You could try -lthisfiledoesnotexist and see for yourself.
    But my experience is that linkers do not typically complain about missing libraries.

    Using LD, the GNU linker
    Try options like --trace and --trace-symbol (see Link Options - Using the GNU Compiler Collection (GCC) for how)
    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 phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    I had to change the correct symbol names for contractual reasons. YOU KNOW that the symbol names aren't the issue, you just decided beating your shest and grunting was the easier response.
    What's this output say to you??

    g++ -Wall -I../somecode/include -static -L../somelib -lsomelib test.cc -o test
    /usr/bin/ld: cannot find -lsomelib
    collect2: ld returned 1 exit status
    make: *** [test] Error 1

    Looks like the linker can't find some lib to me, or does the all mighty man know better??

    Here's the linker output with the correct lib;

    g++ -Wall -I../somecode/include -static -L../thelib -lthelib test.cc -o test
    /tmp/cc6oF9eT.o: In function `main':
    test_file_xml.cpp.text+0xd7): undefined reference to `thelib::XMLFileWriter::XMLFileWriter(char const*)'
    test_file_xml.cpp.text+0x162): undefined reference to `thelib::BasicTextWriter::write(autoserial::ISeria lizable const*)'
    test_file_xml.cpp.text+0x1f4): undefined reference to `thelib::BasicTextWriter::write(autoserial::ISeria lizable const*)'
    (snip)

    I see a difference. Get the crap out of your eyes and stop with the snide, holier than thou nonsense. If you don't want to help then don't reply. I don't need some neckbeard hiding in his mother's basement need to lash out at the poor it girl who "can't figure it". I'll figure it out without your help apparently.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Looks like somebody has "man issues."
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    Sure, I have issues with ..............s. That 99% of them are men? Hey, that's not my doing.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Are you using the same compiler to compile the static library as you are using to compile/link the client code?

    If not, look up name mangling; because you can not mix C++ Compilers and always expect it to work.
    Some GCC C++ versions break compatibilities with others.
    Note, the problem is rare under GCC; but, sometimes it is caused by bugs under GCC.
    But, nearly all different versions of MSVC++ are not name mangling compatible.

    Note: If using an recent GCC 4.x, you might need to read up on visibility options.

    Tim S.
    Last edited by stahta01; 03-19-2012 at 06:19 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    Everything is done with the gnu tool chain, same version, same os (linux mint lisa.) Thank you for a reasoned and even reply.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by phylene View Post
    Sure, I have issues with ..............s. That 99% of them are men? Hey, that's not my doing.
    Actually, you did the following:

    * Assumed the participants in the conversation are of specific genders based on no evidence whatsoever
    * Proceeded to characterize a board moderator as a "neckbeard" because he dared to ask for more detailed information
    * Deliberately pointed out your own gender, as if it's relevant to the topic at hand.

    Basically, you're a sexist.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    Yes, I am a sexist, so what? Comes from years in IT having to deal with sexism. YEARS of it. But throw that aside for a second. My two previous posts were reasoned and DID include infomation indicating the name of the lib had nothing to do with the problem, but neckie there decided to thrown in two useless, time wasting comments;
    Are there any more typos you want to declare before anyone wastes anymore time chasing ghosts?
    Hateful remark, and completely unessessary.
    And;
    But my experience is that linkers do not typically complain about missing libraries.
    Obviously wrong. Neckie is also illinformed. Yes, I took particular joy in pointing that out.
    But I'm the sexist.

  12. #12
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    But you (heretofore to be known as "lezzy") over-reacted freakishly. Nobody cares about your self-proclaimed "years of it". Suck it up, b.....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  13. #13
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    over-reacted freakishly.
    BS. I made two reasonable responsesafter which the dick had to show itself. Don't need it.
    heretofore to be known as "lezzy"
    And quite proud of it, thank you.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Hateful remark, and completely unessessary.
    Unfortunately we know from experience that it is necessary. Too many times, people here have wasted HOURS trying to debug people's code only to find out that the code they posted was typed, from memory, while sitting on a toilet. Salem might have been a bit sarcastic about it, but sarcasm is something you seem familiar with so I'm not sure what your problem is.

    Obviously wrong. Neckie is also illinformed. Yes, I took particular joy in pointing that out.
    No, it is not obviously wrong. Linkers can not possibly know which library provides which symbol. Therefore a linker cannot give an error message of the form "You forgot to link with library X."

    But I'm the sexist.
    You introduced gender into a discussion for no conceivable reason. Honestly, I doubt not only your manners, but your mental stability.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User phylene's Avatar
    Join Date
    Mar 2012
    Location
    Seattle
    Posts
    11
    Unfortunately we know from experience that it is necessary. Too many times, people here have wasted HOURS trying to debug people's code only to find out that the code they posted was typed
    If you read the entire post, which isn't that long, that is obviously not the problem, the case is way too simpllistic for that to be an issue. I have one test case that works, and one, that doesn't. BOTH are ONE app linking to ONE lib. Period. How you extrapolate more out of what was given is beyond me.
    Linkers can not possibly know which library provides which symbol.
    LOOK AT THE CASE! I'm not sure how ANYONE could be confused by that.
    You introduced gender into a discussion for no conceivable reason.
    Yes, I did. THIS is the reason:
    But my experience is that linkers do not typically complain about missing libraries.
    Stupid stupid remark. STUPID.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RPM Depencies and static linking
    By SomeNYCGuy in forum Linux Programming
    Replies: 2
    Last Post: 12-06-2009, 12:58 PM
  2. Removing link libraries in static libs
    By 39ster in forum C Programming
    Replies: 1
    Last Post: 06-10-2008, 10:22 AM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  4. static linking with Dev-c++/gcc
    By silk.odyssey in forum C Programming
    Replies: 6
    Last Post: 12-14-2003, 01:28 PM
  5. Alternative win32 compiler? And linking asm libs?
    By JMB in forum C++ Programming
    Replies: 4
    Last Post: 10-11-2002, 01:22 PM