/* GCC command summary */
Supplement the knowledge of GCC to avoid being told by nature that the compilation principle is not good ..
1.-O Parameters
Parameter description: The-O parameter is used to specify the name of the generated program.
GCC test. C will compile a program named A. Out.
GCC test. C-o Test compiles a program named test.
2.-l parameters and-l Parameters
Parameter description: The-l parameter is used to specify the database to be linked by the program. The-l parameter is followed by the database name. For example, if I want to link to the math database, its database name is M, its library file name is libm. so (generally
/Lib,/usr/libOr
/Usr/local/libDirectory), it is easy to see that the library name is removed from the header lib and tail. So of the library file name.
The libraries in/lib and/usr/local/lib can be directly linked using the-l parameter. However, if the library files are not stored in these three directories, but put it 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.
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 stored in the/usr/bin directory. For example, the MySQL Link parameter Generation Program is mysql_config (note the underline, which is special). Run mysql_config -- libs:
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -ldl
These are the link parameters required by MySQL. It is easy to run the command automatically generated!
In addition to the -- libs parameter, XXX-config also has a parameter -- cflags used to generate the header file containing the directory, that is, the-I parameter. Execute mysql_config -- cflags:
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g
This is the header file directory required by MySQL.
Now the question is how to use these output results. The most direct method is copy, paste, or copy, A better way is to add the 'xxxx-config -- Libs -- cflags 'in the compilation command line, for example, compile a MySQL program: GCC MySQL. c 'mysql _ config -- Libs -- cflags'
OK (I mentioned this in my blog a few days ago, ^ _ ^)
3.-include and-I Parameters
Parameter description:-include is used to contain header files, but generally the header files included are implemented using # include xxxxxx in the source code.-include 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. The -- cflags parameter we mentioned above is used to generate the-I Parameter
4.-O Parameters
Parameter description: This is a program optimization Parameter. Generally,-O2 is used to optimize the program, such as GCC test. c-O2, the optimized program is smaller than the one not optimized, and the execution speed may also be improved.
5.-shared parameters
Parameter description: used to compile a dynamic library, such as gcc-shared test. C-o libtest. So.
6. Several related environment variables (required when cmake is used)
Pkg_config_path: Specifies the path of the PC file used by PKG-config. The default path is/usr/lib/pkgconf.
Ig: The PC file is a text file, and the extension is. PC. It defines the installation path, Libs parameters, and cflags parameters of the Development Kit.
Cc: used to specify the C Compiler
Cxx: used to specify the cxx Compiler
Libs: similar to the above-libs
Cflags: similar to the preceding -- cflags
CC, cxx, Libs, and cflags are not commonly used for manual compilation and are sometimes used for configure.
Skip
Environment variable setting method: Export env_name = xxxxxxxxxxxxxxxxx
7. Reference:Http://wenku.baidu.com/view/655f48b91a37f111f1855b36.html