Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile shared lib with clang on osx

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.

like image 558
giskard Avatar asked Feb 20 '14 12:02

giskard


People also ask

How do you compile in clang?

To compile a C++ program on the command line, run the clang++ compiler as follows: $ scl enable llvm-toolset-6.0 'clang++ -o output_file source_file ...' This creates a binary file named output_file in the current working directory. If the -o option is omitted, the clang++ compiler creates a file named a.

Can you compile GCC with clang?

Clang is a "LLVM native" C/C++/Objective-C compiler using LLVM as a backend and optimizer. It aims to be GCC compatible yet stricter, offers fast compile times with low memory usage, and has useful error and warning messages for easier compile troubleshooting.

What linker does clang use?

Clang can be configured to use one of several different linkers: GNU ld. GNU gold. LLVM's lld.


1 Answers

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.

like image 125
giskard Avatar answered Oct 05 '22 05:10

giskard