C语言 如何在 osx 上使用 clang 编译共享库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21907504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:48:01  来源:igfitidea点击:

How to compile shared lib with clang on osx

cmacosgccshared-librariesclang

提问by giskard

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.

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

回答by giskard

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.

似乎保持 GCC 的相同行为。