c - 如何在 os x 上用 clang 编译共享库

标签 c macos gcc shared-libraries clang

源文件

rsetti::fastidio { /tmp }-> cat foo.c

    #include <stdio.h>
    void ACFunction() {
      printf("ACFunction()\n");
      AGoFunction();
    }

编译共享库

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 }->

同样的代码在linux + gcc上可以轻松编译。

最佳答案

通过使用:

-Wl,-undefined -Wl,dynamic_lookup

clang -shared -undefined dynamic_lookup -o libfoo.so foo.c

似乎保持了 GCC 的相同行为。

关于c - 如何在 os x 上用 clang 编译共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21907504/

相关文章:

c - .c 文件什么时候应该没有关联的 .h 文件?

c - 在 C 中分配指向另一个结构的指针

c - 为什么 char 字符串中没有取消引用运算符?

macos - 如何在 64 位应用程序中从 QTKit 获取 PCM 音频?

macos - 在 Mac 上使用 Shell 脚本出现错误替换错误?

c - 是否可以使用回调类型定义来定义函数?

xcode - 在 NSVisualEffectView 上混合

c++ - 如何打印 C++ 中捕获的异常的堆栈跟踪和 C++ 中的代码注入(inject)

c++ - 用 g++ 编译的奇怪代码

linux - 如何从汇编语言生成可执行文件?(在 emacs 中)