Mixed static and dynamic link on Mac OS

前端 未结 2 1711
轮回少年
轮回少年 2020-12-10 21:25

I want to use gcc to produce a shared library, but i want to link some other libraries it depends on statically. Now to produce the \"standard\" dynamically linked output fi

2条回答
  •  [愿得一人]
    2020-12-10 21:52

    I ran into the same problem. As it turned out, there seems to be no way to link libraries statically without specifying the full path to the .a-file.

    However, there seems to be neat trick in Makefile's allowing for smooth usage.

    vpath %.a /opt/local/lib
    .LIBPATTERNS lib%.a lib%.dylib lib%.so
    
    STATICLIBS = -lssh2
    
    libmy.dylib: my1.o my2.o $(STATICLIBS)
      g++ -dynamiclib -o libmy.dylib $^
    

    Note how the $(STATICLIBS) variable is put in the dependencies. Make will not treat dependecies with an '-l' prefix as files - but rather as libraries. Using the above vpath magic make looks up the libraries and put the full path on the commandline to g++.

提交回复
热议问题