When compiling a program with GCC, parameters such as "-I" (uppercase I), "-l" (uppercase L), and "-l" (lowercase L) may be used, below is a record:
Example:
Gcc-O hello. C-I/home/Hello/include-L/home/Hello/lib-lworld
The above statement indicates that when compiling hello. C:
-I/home/Hello/include indicates that the/home/Hello/include directory is used as the first directory to search for header files. The search sequence is: /home/Hello/include -->/usr/local/includ
-L/home/Hello/lib indicates to use the/home/Hello/lib directory as the first directory to find the library file. The search sequence is: /home/Hello/lib -->/usr/local/lib
-Lworld indicates finding the libworld. So dynamic library file in the Lib path above (if "-static" is added to the GCC compilation option, it indicates searching for the libworld. A static library file)
Gcc-l parameters and-l Parameters
The-l parameter is used to specify the library to be linked by the program, and the-l parameter is followed by the database name. What is the relationship between the database name and the real library file name? Taking the math library for example, his library name is m, and his library file name is libm. So, it is easy to see,Remove the header lib and tail. So of the library file name, that is, the Library name.
Now we know how to get the database name. When we need to use the library name libtest provided by a third party. so, we just need to put libtest. so copy to/usr/lib, add the-ltest parameter during compilation, and we can use libtest. so Library (of course libtest. so library function, we also need to work with libtest. header file of the so configuration set)
Libraries placed in/lib and/usr/local/lib can be linked directly with the-l parameter, however, if the library file is not stored in these three directories, but in other directories, if we only use the-l parameter, the link will still go wrong. The error message is probably: "/usr/bin/ld: cannot find-LXXX", that is, the link program LD cannot find libxxx in the three directories. so, then another parameter-l will be used. For example, the common X11 library is located in the/usr/x11r6/lib directory, we need to use the-L/usr/x11r6/lib-lx11 parameter during compilation. The-l parameter is followed by the Directory Name of the library file. For example, if we put libtest. So in the/AAA/BBB/CCC directory, the Link parameter is-L/AAA/BBB/CCC-ltest.
In addition, most libxxxx. so is just a link. Take rh9 as an example, such as libm. so it links to/lib/libm. so. x,/lib/libm. so.6 link to/lib/libm-2.3.2.so,
If there is no such link, an error will still occur, because LD will only find libxxxx. so, if you want to use the XXXX library, instead of libxxxx. so. X or libxxxx-x.x.x.so, do a link on the Ln-s libxxxx-x.x.x.so libxxxx. so
It is always troublesome to manually write the link parameters. Fortunately, many library sdks provide programs that generate the link parameters. The name is usually XXXX-config, which is usually placed in the/usr/bin directory, for example
The Program for generating the Link parameter of gtk1.2 is GTK-config, run GTK-config -- libs to get the following output: "-l/usr/lib-L/usr/x11r6/lib-lgtk-lgdk-rdynamic-lgmodule-lglib-LDL-LXI ". -lxext-lx11-lm ", this is the GTK Link parameter required to compile a gtk1.2 program. In addition to the -- libs parameter, XXX-config also has a parameter -- cflags used to generate the header file containing directory, that is, the-I parameter, we will talk about it below. Run GTK-config -- Libs -- cflags to check the output result.
The question now is how to use these output results. The most stupid way is to copy, paste, or copy, the clever way is to add the 'xxxx-config -- Libs -- cflags 'in the compilation command line, for example, compile a GTK program: GCC gtktest. c 'gtk-config -- Libs -- cflags. Note that 'is not a single quotation mark, but the key on the left of the 1 key.
5,-Include and-I Parameters
-Include is used to include header files. Generally, the header files contained in the source code are implemented using # include xxxxxx, and the-include parameter is rarely used. -The I parameter is used to specify the header file directory. Generally, the/usr/include directory does not need to be specified. GCC knows where to find it, however, if the header file is not in/usr/include, We need to specify it with the-I parameter. For example, if the header file is placed in the/myinclude directory, the-I/myinclude parameter must be added to the compile command line, if this parameter is not added, you will get a "XXXX. h: No such file or directory "error. -The relative path can be used for the I parameter. For example, if the header file is in the current directory, you can use-I. to specify it.
Differences between GCC "-I" (uppercase I), "-l" (uppercase L), and "-l" (lowercase L)