GCC command line detailed

  

1, gcc contains c/c++ compiler

gcc, cc, c++, g++gcc and cc are the same, c++ and g++ are the same, general c program Compile with gcc, c++ program compile with g++

2, basic usage of gcc

gcc test.c This will compile a program called a.out gcc test.c - o test will compile a program named test -o parameter to specify the name of the generated program

3, why is there an undefined reference to 'xxxxx' error?

First of all, this is a link error, not a compile error. That is to say, if there is only this error, it means that your program source code itself has no problem. It is wrong when you compile with the compiler. You have not specified the linker. To use the library, for example, if you use some math functions in your program, then you must specify the program to link to the math library in the compilation parameters by adding -lm

4 to the compile command line. -l parameter and -L parameter

-l parameter is used to specify the library to be linked by the program, -l parameter is followed by the library name, then what is the relationship between the library name and the real library file name? Take the math library, his library name is m, his library file name is libm.so, it is easy to see that the library file name header lib and tail.so are removed is the library name

Ok, now we know how to get the library name. When we need to use the library name libtest.so provided by a third party, then we just need to copy libtest.so to /usr/lib and add -ltest parameter to compile time. , we can use the libtest.so library (of course, use the functions in the libtest.so library, we also need the header file associated with libtest.so)

put in /lib and /usr/lib The libraries in /usr/local/lib can be linked directly with the -l parameter, but if the library files are not placed in these three directories, but in other directories, then we only use the -l parameter. The link will still be wrong. The error message is probably: <quo;/usr/bin/ld: cannot find -lxxx”, that is, the linker ld can't find libxxx.so in the 3 directories, then another parameter - L comes in handy, such as the commonly used X11 library, which is in the /usr/X11R6/lib directory, we need to use -L/usr/X11R6/lib -lX11 parameters when compiling, - The L parameter is followed by the directory name where the library file is located. 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 to RH9 is an example. For example, libm.so links to /lib/libm.so.x, /lib/libm.so.6 links to /lib/libm-2.3.2.so,

if no Such a link will still go wrong, because ld will only find libxxxx.so, so if you want to use the xxxx library, but only libxxxx.so.x or libxxxx-xxxso, you can make a link to ln -s libxxxx- Xxxso libxxxx.so

Manually writing link parameters is always very troublesome. Fortunately, many library development packages provide programs for generating link parameters. The name is usually called xxxx-config, which is usually placed in /usr/bin. In the directory, for example, the link parameter generator for

gtk1.2 is gtk-config. You can get the following output by executing gtk-config --libs"-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, except xxx-config -- Another parameter outside the libs parameter is --cf Lags is used to generate the header file containing the directory, which is the -I parameter, which we will cover below. You can try to execute gtk-config--libs --cflags to see the output

The problem now is how to use these output results. The most stupid way is to copy and paste or copy, the smart way is Add this `xxxx-config --libs --cflags` to the compile command line, such as compiling a gtk program: gcc gtktest.c `gtk-config --libs --cflags` This is almost the same. Note that `not a single quote, but the one on the left of the 1 key.

In addition to xxx-config, new development packages generally use pkg-config to generate link parameters. The usage is similar to xxx-config, but xxx-config is for a specific development package, but pkg- Config contains the generation of link parameters for many development packages. Use the pkg-config --list-all command to list all supported development packages. The usage of pkg-config is pkg -config pagName --libs --cflags, where pagName Is the package name, which is one of the lists listed in pkg-config--list-all. For example, the name of gtk1.2 is gtk+, pkg-

config gtk+ --libs --cflags and gtk -config --libs --cflags is the same. For example: gcc gtktest.c `pkg-config gtk+ --libs --cflags`

5, -include and -I parameters

-include is used to include header files, but in general The include header files are implemented in the source code using #include xxxxxx, and the -include parameter is rarely used. -I parameter is used to specify the header file directory, /usr/include directory is generally not specified, gcc knows to go there to find, but if the header file is not in /usr/include we have to use the -I parameter to specify, such as the header The file is placed in the /myinclude directory. The compile command line will add the -I/myinclude parameter. If you don't add it, you will get a "xxxx.h: No such file or directory" error. The -I parameter can use a relative path, such as the header file in the current directory, which can be specified with -I. The --cflags parameter we mentioned above is used to generate the -I parameter.

6, -O parameter

This is a program optimization parameter, generally used -O2 is used to optimize The program used, such as gcc test.c -O2, the optimized program is smaller than the unoptimized, the execution speed may also be improved

7, the -shared parameter is used when compiling the dynamic library, such as gcc -shared test.c -o libtest.so

8, several related environment variables PKG_CONFIG_PATH: used to specify the path of the pc file used by pkg-config, the default is /usr/lib/pkgconf

Copyright © Windows knowledge All Rights Reserved