Force static linking of library linked to Xcode target?

前端 未结 5 664
别跟我提以往
别跟我提以往 2020-12-09 19:06

My Xcode target links against hdf5 library (using the Link Binary with Libraries build phase). libhdf5 is installed using MacPorts, thus /opt/local/lib contains both the dyn

相关标签:
5条回答
  • 2020-12-09 19:33

    My case with Xcode 4.5:

    When I drag and drop a static C library (a 3rd party library compiled with GNU Autotools) to project's frameworks (Project Navigator > Frameworks) the linker seems to thinks that's a dynamic library and adds -L -l flags:

    -L/path/to/libfoodir -lfoo
    

    The linking fails because there is no /path/to/libfoodir/libfoo.dylib. The linker command can be seen from:

    Log Navigator > select a Build log > select a Link line and expand it
    

    The linking succeeds when I add a full path (/path/to/libfoodir/libfoo.a) to the linker settings:

    Targets > Build Settings (all) > Other linker flags
    
    0 讨论(0)
  • 2020-12-09 19:33

    Under XCode 11.3.1, targeting MacOS

    Tried to add absolute lib path to Other linker flags. But that doesn't work. So I copied required *.a files (ln -s may also work) to project dir and it worked like a charm.

    0 讨论(0)
  • 2020-12-09 19:35

    Use the "-static" switch for linking: GCC link options

    0 讨论(0)
  • 2020-12-09 19:39

    In reaction to your comment on Eduard Wirch' answer: you can also control static linking for this one library only, if you replace -lhdf5 by -l/full/path/to/libhdf5.a

    0 讨论(0)
  • 2020-12-09 19:54

    Had this exact same problem and despite this being an old post, I thought I'd share what I had to do to make this work.

    Usually you do just provide the switch '-static' to the linker however, with Xcode this causes all libs including the crt to be linked statically. I got the error:

    can't locate file for: -lcrt0.o
    

    When I tried this.

    The thing which worked for me was to replace:

    -lmylib
    

    with

    /path/to/libmylib.a

    Note: the -l is dropped.

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