Mixing static libraries and shared libraries

后端 未结 1 1809
一个人的身影
一个人的身影 2020-12-10 17:41

I have a project where I have one static library libhelper.a and another with my actual shared object library, libtestlib.so. My goal is to link

相关标签:
1条回答
  • 2020-12-10 18:31

    My goal is to link libhelper.a into libtestlib.so. Is that possible on Linux?

    Sure. This should do:

    gcc -shared -fPIC -o libtestlib.so $(OBJS) \
      -Wl,--whole-archive -lhelper -Wl,--no-whole-archive
    

    libhelper.a was not compiled with -fPIC

    It's best to rebuild libhelper.a with -fPIC. If that's not possible, above command will still work on Linux/ix86, but not on e.g. Linux/x86_64.

    What is the proper way to build programs that use shared libraries that also have dependancies on static libraries?

    If you include libhelper.a into libtestlib.so as above, then simple:

    gcc main.c -ltestlib
    

    is all you need. If you insist on linking with libhelper.a, then you must tell the end-user that he must link with e.g.

    gcc main.c -ltestlib -lhelper
    

    There is no way to specify that libtestlib.so depends on libhelper.a.

    0 讨论(0)
提交回复
热议问题