OSX: How do I convert a static library to a dynamic one?

后端 未结 3 548
星月不相逢
星月不相逢 2021-02-13 10:35

Suppose I have a third party library called somelib.a on a Mac running Mountain Lion with Xcode 4.4 installed. I want to get a dynamic library out of it called somelib.dylib. An

相关标签:
3条回答
  • 2021-02-13 10:48

    Note: A link for the documentation of the OSX ld linker.

    http://www.unix.com/man-page/osx/1/ld/

    I know it is late to give an answer for this, but I do not have enough reputation to make a comment on @hanslovsky answer. However, it helps me a lot to have the docs of the options too. It helps what the options do exactly, and that other options the ld linker also has. So I just wanted to share with others who finds linking an issue.

    UPDATE:

    After the comment from @GhostCat I have decided to expand my answer.

    The docs for -all_load is:

    -all_load

    Loads all members of static archive libraries.
    

    So it loads for all static libraries that you note. If you want something similar to --whole-archive and --no-whole-archive, then you need to use -force_load and -noall_load.

    -force_load "path_to_archive"

    Loads all members of the specified static archive library. Note: - all_load forces all members of all archives to be loaded.
    This option allows you to target a specific archive.

    -noall_load

    This is the default. This option is obsolete.

    Then you can define which libraries to fully load with -force_load and then later turn it off again with -noall_load.

    0 讨论(0)
  • 2021-02-13 10:48

    According to the ld manual, -noall_load is the default and is ignored. (If you use it, you get an error message: ld: warning: option -noall_load is obsolete and being ignored)

    Apparently the way to get -all_load to apply to only one library is as follows:

    -Wl,-force_load,somelib.a
    
    0 讨论(0)
  • 2021-02-13 11:08

    I found out the solution to my problem:

    g++ -fpic -shared -Wl,-all_load somelib.a -Wl,-noall_load -o somelib.dylib
    

    The required arguments are -all_load and -noall_load.

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