Results 1 to 11 of 11

Thread: Linking a static library with a shared library.

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Linking a static library with a shared library.

    Hey,

    How would I go about linking my static library with a shared library?

    Here's an example:
    Say I have a simple static library that provides one method:
    Code:
    #include <GL/gl.h>
    
    void bar()
    {
    	glFlush();
    }
    All it does is call glFlush.
    For this, the library needs to link to libGL. So this is what my Makefile looks like:

    Code:
    all : foo.o
    	ar cq libfoo.a foo.o
    foo.o : foo.cpp
    	g++ -Wall -pedantic foo.cpp -o foo.o -lGL
    
    clean:
    	rm -f *.o
    	rm -f *.a
    But what happens when i make this, is getting a nice undefined reference to 'main' error.
    I guess this is because I would normally would provide g++ with the -c option to skip linking, but this time I want to link with libGL so I had to remove it...and I am guessing this causes g++ to link with a number of default libraries, which expect my code to define a main function. Just guessing.

    Another solution I have found is to not link with libGL at that stage, instead add the -c option, and then when the static library in turn is linked with the final executable I link with libGL. This works. However, I am really hoping I can avoid this because I dont want my "end users" to have to link with every single shared library that my static library refers to.

    I hope this question makes sense, if not, tell me.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Linking a static library with a shared library.

    Surely someone must be able to shed some light on this? If I am unclear do say so and I will attempt to rephrase my question.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    23

    Re: Linking a static library with a shared library.

    The error is defined as an undefined reference to main? If you are referencing "main", make sure "main" exists.

    Could you maybe rephrase your question, it is leaving me a bit confused as to the issue you are having. I know you happen to be using some form of Linux for this, am I correct?

  4. #4

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Linking a static library with a shared library.

    I'm not referencing main anywhere, the only code that exists is the one in the first post.
    However, after some googling, it seems like what I posted at the end of my first post is what has to be done.
    If one performs the linking stage on a static library, I am guessing that it is linked with some default library that requires the presence of a 'main' entrypoint.
    The solution is: Do not perform linking when building a static library (-c option in gcc), and instead link to the relevant external libraries in the final executable.

    I had hoped this could be avoided, because when people link to my library they're going to have to link to a whole heap of shared libraries, or else they'll get compile errors.
    I am using Linux. Fedora to be precise.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    23

    Re: Linking a static library with a shared library.

    Ah, so basically you were trying to avoid having the users link to a ton of shared libraries?
    Does main exist at all? Generally, compilers assume there is a function called "main", and so it will try and run main regardless, if there is no main, you must define that something that isn't called "main" shall function as main, or the compiler can have a starting point.

  6. #6

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Linking a static library with a shared library.

    Yeah that's pretty much it.
    Surely that can't be the case, can it? An entrypoint shouldnt be needed in a static library, seeing as it is only just a collection of object files? Also, why would the compiler try to run main?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    23

    Re: Linking a static library with a shared library.

    I'm not sure as to why a compiler would attempt to run main, but it happens in some cases. Some compilers are poorly made and don't "know" what to do when main doesn't exist. Even for a static library, your compiler still wants an "entry point", and if it cannot find what it qualifies as an entry point, it may bring up an error.
    While not the most likely reason you'd be having problems, it is a possibility worth looking into. When you run it, try using "c++" instead of "g++", I'm unsure of the difference, but on my Linux system, I have occasionally solved problems by doing so. Though, I'm pretty sure you will have to use "sudo apt-get install c++" beforehand to get it.

    I'll look into your problem and see if I can think of any solution or reason for the problem.

  8. #8

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Linking a static library with a shared library.

    I must say that I still stand by my point that a static library does not need a main entry point. And surely GCC can not be considered a poorly made compiler.

    I am beginning to accept that end users will have to link to the libraries needed by my library in order to use my library (that sentence confused me a little! ), I guess thats nust how stuff has to work. I guess it is logical when you think about it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9
    Junior Member
    Join Date
    May 2011
    Posts
    23

    Re: Linking a static library with a shared library.

    I don't think GCC is a poorly made compiler, I just think that, like every other compiler, it has its little flaws.
    I actually like GCC, but for some reason I tend to compile with the statement "c++", instead of "g++". I don't actually know if there is a difference.

    Ah well, your end users can deal with linking to the libraries needed by your library to use your library. That is a pretty logical solution now that I think of it, it may not be the easiest solution, but it is the only way so far as I can tell.
    And besides, it's no skin of your back if your users have troubles, they are the ones screwing up and not linking to the libraries and having the troubles.

  10. #10
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: Linking a static library with a shared library.

    You have an error in your Makefile:

    Code:
    foo.o : foo.cpp
    	g++ -Wall -pedantic foo.cpp -o foo.o -lGL
    should be this instead:
    Code:
    foo.o : foo.cpp
    	g++ -Wall -pedantic -c foo.cpp -o foo.o -lGL
    Dont forget gcc or g++ always tries to build a binary containing a main() entry point. The -c flag tells which ones are source files.

    Also, gcc is one of the professionally made compilers with way better compiler optimizations, architecture support and many other features hardly other compilers provide

    BTW I came across this topic while searching for something else, and I thought I'll register and post a reply

  11. #11
    New Member
    Join Date
    Jul 2011
    Posts
    2

    Re: Linking a static library with a shared library.

    Yeah forgot to mention (double posting because I'm unable to edit the previous post) - the presence of -c flag invokes only the gcc c or c++ compiler and not the complete gcc toolchain.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width