newbieMACuser
newbieMACuser

Reputation: 907

Convert .a to .dylib in Mac osx

Is it possible to convert .a files to .dylib files in Mac osx? I currently have libraryname.a and it can't seem to include it in my program as only .dylib libraries are included.

Is there also a command that shows static libraries used in a program via mac osx terminal?

Upvotes: 9

Views: 4210

Answers (1)

Stuart Berg
Stuart Berg

Reputation: 18162

Yes, this is possible. To convert foo.a into libfoo.dylib, try this command:

clang -fpic -shared -Wl,-all_load foo.a -o libfoo.dylib

On Linux, here's the equivalent command using gcc:

gcc -fpic -shared -Wl,-whole-archive foo.a -Wl,-no-whole-archive -o foo.so

Here's a complete example.

Let's start by creating (and testing) libfoo.a:

$ cat > foo.h
int foo();

$ cat > foo.c
int foo() {
  return 42;
}

$ cat > main.c
#include "foo.h"
int main() {
  return foo();
}

$ clang -c foo.c -o foo.o
$ ar -r libfoo.a foo.o
ar: creating archive libfoo.a

$ clang libfoo.a main.c -o main.out
$ ./main.out; echo $?
42

Now let's convert it into libbar.dylib and test again:

$ clang -fpic -shared -Wl,-all_load libfoo.a -o libbar.dylib
$ clang -L. -lbar main.c -o main.out
$ ./main.out; echo $?
42

Upvotes: 7

Related Questions