Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 573
newbieMACuser Avatar asked Aug 15 '14 06:08

newbieMACuser


1 Answers

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
like image 62
Stuart Berg Avatar answered Oct 18 '22 10:10

Stuart Berg