Convert .a to .dylib in Mac osx

旧城冷巷雨未停 提交于 2020-12-08 07:09:29

问题


Is it possible to convert .a files to .dylib files in Mac osx? I currently have libraryname.a and it can't seem to include it in my program as only .dylib libraries are included.

Is there also a command that shows static libraries used in a program via mac osx terminal?


回答1:


Yes, this is possible. To convert foo.a into libfoo.dylib, try this command:

clang -fpic -shared -Wl,-all_load foo.a -o libfoo.dylib

On Linux, here's the equivalent command using gcc:

gcc -fpic -shared -Wl,-whole-archive foo.a -Wl,-no-whole-archive -o foo.so

Here's a complete example.

Let's start by creating (and testing) libfoo.a:

$ cat > foo.h
int foo();

$ cat > foo.c
int foo() {
  return 42;
}

$ cat > main.c
#include "foo.h"
int main() {
  return foo();
}

$ clang -c foo.c -o foo.o
$ ar -r libfoo.a foo.o
ar: creating archive libfoo.a

$ clang libfoo.a main.c -o main.out
$ ./main.out; echo $?
42

Now let's convert it into libbar.dylib and test again:

$ clang -fpic -shared -Wl,-all_load libfoo.a -o libbar.dylib
$ clang -L. -lbar main.c -o main.out
$ ./main.out; echo $?
42


来源:https://stackoverflow.com/questions/25321911/convert-a-to-dylib-in-mac-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!