Reputation: 11
same options can be used for both statically linking and dynamically linking a library. $(CC) $(CFLAGS) -L$(PATH_TO_LIB) -l$(library name) -o output-file-name
If my PATH_TO_LIB contains both the .a and .so file, which one will linker prefer to?
Upvotes: 1
Views: 364
Reputation: 170045
If the linker is a variant of ld then it will prefer shared objects over archives. From the documentation of the -l
flag, emphasis mine:
-l namespec
--library=namespecAdd the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.
On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.
Upvotes: 1