如何在osx上用clang编译共享库 [英] How to compile shared lib with clang on osx
本文介绍了如何在osx上用clang编译共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
rsetti :: fastidio {/ tmp} - > cat foo.c
#include< stdio.h>
void ACFunction(){
printf(ACFunction()\\\
);
AGoFunction();
}
编译共享库
rsetti :: fastidio {/ tmp} - > clang -shared -o libfoo.so foo.c
foo.c:4:3:警告:函数的隐式声明'AGoFunction'在C99中无效[-Wimplicit-function-declaration]
AGoFunction();
^
1生成警告。
架构x86_64的未定义符号:
_AGoFunction,引用自:
foo-lFDQ4g.o中的_ACFunction
架构x86_64未找到ld:symbol(s)
clang:错误:linker命令失败,退出代码1(使用-v查看调用)
rsetti :: fastidio {/ tmp} - >
linux + gcc上的相同代码可以轻松编译。
解决方案
通过使用:
-Wl,-undefined -Wl,dynamic_lookup
或
clang -shared -undefined dynamic_lookup -o libfoo.so foo.c
似乎保持GCC的相同行为。
source file
rsetti::fastidio { /tmp }-> cat foo.c
#include <stdio.h>
void ACFunction() {
printf("ACFunction()\n");
AGoFunction();
}
compilation of shared lib
rsetti::fastidio { /tmp }-> clang -shared -o libfoo.so foo.c
foo.c:4:3: warning: implicit declaration of function 'AGoFunction' is invalid in C99 [-Wimplicit-function-declaration]
AGoFunction();
^
1 warning generated.
Undefined symbols for architecture x86_64:
"_AGoFunction", referenced from:
_ACFunction in foo-lFDQ4g.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
rsetti::fastidio { /tmp }->
the same code on linux + gcc can be easily compiled.
解决方案
By using:
-Wl,-undefined -Wl,dynamic_lookup
or
clang -shared -undefined dynamic_lookup -o libfoo.so foo.c
seems to maintain the same behaviour of GCC.
这篇关于如何在osx上用clang编译共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文