Like a static link to OS X

I am trying to establish a link to a static library in OS X. I used the -static flag in the gcc command, but I get the following error message:

 ld_classic: can't locate file for: -lcrt0.o
 collect2: ld returned 1 exit status

I looked at the manual pages and read something like:

This option will not work on Mac OS X unless all libraries (including libgcc.a) have also been compiled with -static. Since neither the static version of libSystem.dylib nor crt0.o is provided, this option is not suitable for most people.

Is there any other way to link to this static library?

+41
linker static-libraries macos
May 10 '09 at 6:22 a.m.
source share
5 answers

To connect to the archive library (sometimes also called the static library), simply add it to the link line:

 gcc main.o ... -lfoo ... 

The linker will be looking for libfoo.dylib, and then libfoo.a, which you need.

If you have both versions of the library and want to associate them with the archive version, preferring the dynamic one, simply indicate the full path to the archive on the link line:

 gcc main.o ... /path/to/libfoo.a ... 
+53
May 10, '09 at 21:54
source share

Unfortunately, it is not supported . Some people have reported that crt0 can be compiled manually, but no one confirms this .

+12
Dec 04 '09 at 16:33
source share

-Bstatic does not seem to work on OS-X Lion - gcc -v is used to confirm this.

+7
Aug 07 2018-12-12T00:
source share

A common case is a static link to a third user library when dynamically linked to system frameworks and libraries, so your users do not need to install third-party libraries before using your program. If a library is dynamically linked to frameworks (as is often the case), it can still come with a static .a, but just replacing -l<libname> with /path/to/libname.a not enough, because .a will not have dependencies in that. You will also have to dynamically link to the frameworks that your library used.

For example, let's say you want to write a program that uses open source libusb without requiring the user to download and install libusb. Let's say you have a dynamically linked bination that you created with this:

 clang -lusb-1.0 main.c -o myprogram 

To statically reference OS X, the command looks like this (note the -framework arguments):

 clang -framework CoreFoundation -framework IOKit main.c /path/to/libusb-1.0.a -o myprogram 

To find out which system framework and libraries you need to add, look at a third-party dylib using otool:

 otool -L /usr/local/opt/libusb/lib/libusb-1.0.0.dylib 

which shows:

 /usr/local/opt/libusb/lib/libusb-1.0.0.dylib: /usr/local/opt/libusb/lib/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0) /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0) /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0) /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1348.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0) 

You can start by adding frameworks followed by libraries one at a time, and you will see that the list of undefined errors is reduced. Note that you probably will not need to add each library, because some may be loaded as dependencies for those that you explicitly added.

If you do not know where dylib is located, create your program in the original dynamic way (with -lusb-1.0) and run otool on it:

 clang -lusb-1.0 main.c -o myprogram otool -L myprogram 

which gives:

 myprogram: /usr/local/opt/libusb/lib/libusb-1.0.0.dylib (compatibility version 2.0.0, current version 2.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0) 

Also read the license of the library you are referring to.

+4
Jan 06 '17 at 21:39 on
source share

I ran into the same problem. Here is an example:

STEP1: creating files

myfunc1.c:

 #include <stdio.h> void myfunc1() { printf( "This is my func1!\n" ); } 

myfunc2.c:

 #include <stdio.h> void myfunc2() { printf( "This is my func2!\n" ); } 

and myfunc.c:

 #include <stdio.h> void myfunc1( void ); void myfunc2( void ); int main() { myfunc1(); myfunc2(); return 0; } 

STEP2: create lib

 gcc -c myfunc1.c myfunc2.c ar -r libmyfuncs.a myfunc1.o myfunc2.o 

STEP3: link

 gcc -o myfunc -L. myfunc.c -lmyfuncs 

Remember to enter "-L."; the dot indicates the current path.

Hope this helps.

0
Jun 11 '17 at 15:21
source share



All Articles