【问题标题】:How to compile shared lib with clang on osx如何在 os x 上使用 clang 编译共享库
【发布时间】:2014-03-21 09:04:38
【问题描述】:

源文件

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 上的相同代码可以轻松编译。

【问题讨论】:

标签: c macos gcc shared-libraries clang


【解决方案1】:

通过使用:

-Wl,-undefined -Wl,dynamic_lookup

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

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

【讨论】:

  • 成功了。我知道这一定很简单。继续!
  • .dylib 是 macOS 上共享库的规范扩展,使用 -dynamiclib 而不是 -shared。见stackoverflow.com/questions/2339679/…
  • 请注意,“-undefined dynamic_lookup”会产生不必要的副作用,例如当您的类中有一些未定义的符号时,编译器诊断会被静音。这些未定义的符号稍后会在尝试在运行时加载库时弹出,并出现误导性错误,例如“dlopen:找不到图像”。详情请见stackoverflow.com/a/30934307/71689
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 2020-01-04
  • 2011-03-21
  • 2013-09-25
  • 2012-04-08
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
相关资源
最近更新 更多