Convert a Static Library to a Shared Library?

前端 未结 5 1208
情歌与酒
情歌与酒 2020-11-28 03:02

I have a third-party library which consists mainly of a large number of static (.a) library files. I can compile this into a single .a library fil

相关标签:
5条回答
  • 2020-11-28 03:13
    ar -x lib***.a
    gcc -shared *.o -o lib***.so
    
    0 讨论(0)
  • 2020-11-28 03:15

    Does this (with appropriate -L's of course)

    gcc -shared -o megalib.so foo.o bar.o -la_static_lib -lb_static_lib
    

    Not do it?

    0 讨论(0)
  • 2020-11-28 03:15

    You can't do this if objects within static library was compiled without -fPIC or like.

    0 讨论(0)
  • 2020-11-28 03:17
    g++ -shared -o megalib.so foo.o bar.o -Wl,--whole-archive -la_static_lib -lb_static_lib -Wl,--no-whole-archive -lc_static_lib -lother_shared_object
    

    I'm not sure about gcc, but for g++ I had to add the --whole-archive linker option to include the objects from the static libraries in the shared object. The --no-whole-archive option is necessary if you want to link to libc_static_lib.a and libother_shared_object.so, but not include them as a whole in megalib.so.

    0 讨论(0)
  • 2020-11-28 03:27

    ar -x can be also useful if you want to focus on specific objects from your .as and you don't want to add anything on your own.

    Examples:

    ar -x lib***.a
    gcc -shared *.o -o lib***.so
    
    0 讨论(0)
提交回复
热议问题