在Mac OS X中将.a转换为.dylib [英] Convert .a to .dylib in Mac osx

查看:209
本文介绍了在Mac OS X中将.a转换为.dylib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Mac osx中是否可以将.a文件转换为.dylib文件?
我目前有libraryname.a,它似乎无法将其包含在我的程序中,因为仅包含.dylib库。

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.

是否还有一个命令可以显示通过Mac OS X终端在程序中使用的静态库?

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

推荐答案

是的,这是可能的。要将 foo.a 转换为 libfoo.dylib ,请尝试以下命令:

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

在Linux上,这是使用 gcc 的等效命令:

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

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

这是一个完整的示例。

让我们从创建(和测试)开始 libfoo.a

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

现在让我们将其转换为 libbar.dylib 并再次测试:

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

这篇关于在Mac OS X中将.a转换为.dylib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆