If you have to use the `-llibary` option in clang

0

Wondering if you have to use the -llib option in clang. For instance:

clang -lasdf foo.c -o foo.o

will link library /usr/local/lib/libasdf.dylib to foo.c, but wondering if there is any way to not have to include it, so it works like other system libraries, where you can just do #include <math.h> and don't have to link anything.

Or perhaps that's what they're talking about here:

The libSystem library also includes functions that you would normally expect to find in libc and libm, RPC services, and a name resolver. Because libSystem is automatically linked into your application, you do not need to explicitly add it to the compiler’s link line. For your convenience, many of these libraries exist as symbolic links to libSystem, so while explicitly linking against -lm (for example) is not needed, it will not cause an error

Lance Pollard

Posted 2019-03-28T14:59:52.447

Reputation: 125

Answers

0

From the manual page of Apple's ld, which the -llib option is passed to:

-lx This option tells the linker to search for libx.dylib or libx.a in the library search path. If string x is of the form y.o, then that file is searched for in the same places, but without prepending 'lib' or appending '.a' or '.dylib' to the filename.

From this, it doesn't seem like there's any way to not use -l because all the flag does is tell the linker to look for a library and then link it into the executable. It is not automatically included because it is not part of the libSystem library that you referenced, which appears to be the only automatically linked library on macOS. Libraries in /usr/local/lib/ (at least on MacOS) are not system libraries, in the sense that they would be automatically linked, because /usr/local/lib is generally used for user installed libraries.

Kevin Rockwell

Posted 2019-03-28T14:59:52.447

Reputation: 169