8

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?

newbieMACuser
  • 847
  • 1
  • 11
  • 19

1 Answers1

7

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
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • is the reverse possbile? I mean can I have .a file for .dylib on macos? – Akil Demir Jan 27 '20 at 13:42
  • 1
    I'm not sure, but some quick searching doesn't make me hopeful. If you try feeding `libfoo.dylib` into `ar`, it complains. Also, `man ar | grep dynamic` returns nothing. (Not a good sign.) And [this answer](https://stackoverflow.com/a/22575470/162094) claims it's not possible. – Stuart Berg Jan 27 '20 at 15:43