Andrew Arrow
Andrew Arrow

Reputation: 4565

How can I statically link a dylib to my program on macOS?

my c program calls:

hLibrary = dlopen("libPCBUSB.dylib", RTLD_LAZY);

and I seem to need this file in the directory when I run the executable after calling gcc main.c.

i.e. I run ./a.out and it all works as long as the dylib is in that directory.

In order to produce an executable with that dylib statically built in I've been trying all sorts of linking options but failing.

What is the correct way to compile my c program (in macOS Darwin not linux) to include this lib so the end user will not need it on their Mac?

Upvotes: 2

Views: 4168

Answers (1)

pmdj
pmdj

Reputation: 23428

Dynamic libraries (.dylib) can't be statically linked. If you have access to the source code for building the library, you can convert it to a static library and statically link against it in your app. If this is a 3rd-party binary-only library, you will need to ask the vendor for a static version of the library, and if that's not available, you will need to stick with linking it dynamically.

Note that dlopen() is not the only way to link against a dylib, you can also use -l, then you don't need to mess around with dlsym() etc. to get to the entry points. Either way requires shipping the library with your app of course.

Upvotes: 3

Related Questions