How to Statically Link Standard Library to My C++ Program

OSX: How to statically link a library and dynamically link the standard library?

Put your static libraries in, say, dir ./MyStaticLibs and simply use -L./MyStaticLibs/ -l<StaticLibraryName>.

Compiler prefers dynamic version over static version of library

If you have your dynamic library (random.dylib) and static library (random.a) in same directory then compiler will prefer and link with .dylib not .a

Linking static libraries to other static libraries

Static libraries do not link with other static libraries. The only way to do this is to use your librarian/archiver tool (for example ar on Linux) to create a single new static library by concatenating the multiple libraries.

Edit: In response to your update, the only way I know to select only the symbols that are required is to manually create the library from the subset of the .o files that contain them. This is difficult, time consuming and error prone. I'm not aware of any tools to help do this (not to say they don't exist), but it would make quite an interesting project to produce one.

Where/When to link against the Standard C Library when compiling C programs?

Essentially every hosted C implementation requires you to link programs with an implementation of the standard C library.

Some commands for building programs have the inclusion of the standard C library built-in as a default, so you do not need to add it explicitly on the command library. Some commands do not have it as a default, so you do need to add it on the command line.



Related Topics



Leave a reply



Submit