Differencebetween -I and -L in Makefile

Makefile -L command

-L in this context is an argument to the linker, that adds the specified directory to the list of directories that the linker will search for necessary libraries, e.g. libraries that you've specified using -l.

It isn't a makefile command, even though it's usually seen in makefiles for C projects.

-L and -l commands don't work in Makefile

From ld(1):

-l namespec
--library=namespec
Add 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.

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

See the -( option for a way to force the linker to search archives multiple times.

You may list the same archive multiple times on the command line.

This type of archive searching is standard for Unix linkers. However, if you are using ld on AIX , note that it is different from the behaviour of the AIX linker.

Your library is called VirtualBank.a, but ld will look for libVirtualBank.a.

Compiling dynamically linked library in a makefile


LD_LIBRARY_PATH=.
export LD_LIBRARY_PATH

These two lines do not influence the creation of the program in any way because you type the lines after creating the program.

These lines are not used for building your program, but they influence running the program (by typing ./out).

If you compile your program using gcc directly (not using make) and open a new terminal, you also have to type these two lines (again) before you run the program.

It does not matter how you build the program (by typing gcc manually or by running make):

After opening a new terminal, you will need to type these two lines before you run the program.

However, the dynamic linker does not only use the path information from LD_LIBRARY_PATH but also from the DT_RUNPATH information in the executable.

Unlike the LD_LIBRARY_PATH variable which is set on one console (or terminal) only, the DT_RUNPATH information is stored directly in the executable file.

As described in another question on this site, you can set the DT_RUNPATH information using the -Wl,-rpath=<value> switch:

gcc -L. main1.c -lname -o out -Wl,-rpath=.

If you do this, the dynamic linker will search your library (libname.so, if I understand correctly) in the current directory.

Note:

. really means: In the current directory; it does not mean: In the same directory as the executable file!

If your program is stored in the directory ./somedir and you type somedir/out, the file ./libname.so is searched, not the file ./somedir/libname.so.

This is both the case for the -Wl,-rpath= method and for the LD_LIBRARY_PATH= mehtod.



Related Topics



Leave a reply



Submit