Http://www.360doc.com/content/11/1024/22/1671317_158816062.shtml
GCC command line explanation-L specifies the path of the database-L specifies the name of the database to be connected 1. C/C ++ compiler included in GCC
GCC, CC, C ++, G ++, GCC and CC are the same. c ++ and G ++ are the same.
(:)) C Programs are compiled using gcc, and C ++ programs are compiled using G ++.
2. Basic GCC usage
GCC test. C will compile a program named A. Out.
GCC test. C-O test will compile a program named test. The-O parameter is used to specify the name of the generated program.
Word
3. Why does the undefined reference to 'xxxxx' error occur?
First, this is a link error, not a compilation error. That is to say, if only this error occurs, it indicates your program source code.
There is no problem. It is because the parameters are incorrect when you compile with the compiler. You didn't
A library is required for a specified link program. For example, if some mathematical functions are used in your program, you need to compile
Specify the program in the parameter to link to the math library by adding-lM to the compilation command line.
4. -L 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 component names?
Taking the math library for example, his library name is m and his library file name is libm. so. It is easy to see that the library file name
The Library name is removed from the header lib and tail. So.
Now we know how to get the library name. For example, we need to use a third-party library named Lib.
Test. So, we only need to copy libtest. So to/usr/lib.
When the-ltest parameter is added during compilation, we can use the libtest. So Library (of course, we must use the libtest. So library ).
Function, we also need header files matching libtest. So ).
Put in/LibAnd/Usr/libAnd/Usr/local/libThe Library in can be linked directly with the-l parameter, but if the library file
Not in these three directories, but in other directories.
If you only use the-l parameter, the link will still fail. The error message is probably "/usr/bin/ld: cannot find ".
-LXXX ", that is, the linked program LD cannot be found in the three directories.
Libxxx. So, then another parameter-l will be used, such as the commonly used X11 library, which is put in/usr/x11r
6. In the lib directory, we need to use-L/usr/x11r6/lib-
Lx11 parameter. The-l parameter is followed by the Directory Name of the library file. For example, we put libtest. So in/AAA/BB.
In the B/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. For example, libm. So, it is linked to/lib/libm. S.
O. X,/lib/libm. so.6 and link to/lib/libm-2.3.2.so,
If there is no such link, there will still be an error, because LD will only find libxxxx. So, if you want to use xxxx
Library, while only libxxxx. So. X or libxxxx-x.x.x.so, do
Link to the Ln-s libxxxx-x.x.x.so libxxxx. So
It is always troublesome to manually write the link parameters. Fortunately, many libraries provide programs that generate the link parameters.
Usually XXXX-config, which is stored 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 ", which is the G required to compile a gtk1.2 Program
TK Link parameter. In addition to the -- libs parameter, XXX-config also has a parameter.
Number is -- cflags used to generate the header
Contains the directory, that is, the-I parameter, which will be discussed below. You can try to execute GTK-config
-- Libs -- cflags to check the output result.
Now the question is how to use these output results. The most stupid way is to copy, paste, and copy the results.
Add 'xxxx-config -- Libs -- in the compilation command line --
Cflags '. For example, compile a GTK program: GCC gtktest. c 'gtk-config -- Libs -- cflags '.
Poor
Not much. Note that 'is not a single quotation mark, but the key on the left of the 1 key.
In addition to XXX-config, the new development kit usually uses PKG-config to generate link parameters.
Similar to XXX-config, XXX-config is a specific development kit.
But PKG-config contains the generation of many development kit link parameters. You can use the PKG-config -- list-all command
List all supported sdks. The usage of PKG-config is PKG.
-Config pagname -- Libs -- cflags, where pagname is the package name and PKG-config -- list-all
List one of the list. For example, gtk1.2 is named GTK +, PKG-
The role of config GTK + -- Libs -- cflags is the same as that of GTK-config -- Libs -- cflags. For example:
GCC gtktest. c 'pkg-config GTK + -- Libs -- cflags'
.
5. -Include and-I Parameters
-Include is used to include header files, but generally including header files are implemented in the source code using # include xxxxxx.
The-include parameter is rarely used. -The I parameter is used to specify the header file directory.
, The/usr/include directory is generally not specified, GCC knows to find it, but if the header file is not in/usr/I
We need to use the-I parameter to specify the nclude, such as the header file
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
An error "XXXX. h: no such file or directory" occurs. -I
You can use relative paths for parameters. For example, if the header file is in the current directory, you can use-I. to specify the parameters. What we mentioned above -- cf
The lags parameter is used to generate the-I parameter.
6. -O Parameters
This is a program optimization Parameter. Generally,-O2 is used to optimize the program, such as GCC test. C-O2.
The resulting programs are smaller than those not optimized, and the execution speed may also be improved.
High (I have not tested ).
7. -Shared Parameters
Used to compile dynamic libraries, such as gcc-shared test. C-o libtest. So.
8. Several related environment variables
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, which defines development.
Package installation path, Libs parameters, and cflags parameters.
Cc: used to specify the C compiler.
Cxx: used to specify the cxx compiler.
Libs: similar to 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.
Do not worry.
Environment variable setting method: Export env_name = xxxxxxxxxxxxxxxxx
9. Cross-Compilation
In general, cross-compilation is to compile and compile on one platform that can run on another platform with different architectures.
For example, on our local PC platform (x86 CPU), compile and compile
For programs on the CPU platform, compiled programs cannot run on the x86 CPU platform and must be placed in
CPU platform.
Of course, both platforms use Linux.
This method is widely used in different platform transplantation and embedded development.
Compared with cross-compilation, we usually compile locally, that is, compile on the current platform.
The program is also executed locally.
The compiler used to compile such a program is called the cross compiler. The Compiler used for local compilation is called the local compiler.
Generally, GCC is used, but this GCC is used with the local GCC compiler.
It is different. You need to use a specific configure parameter during GCC compilation to obtain GCC that supports cross-compilation.
In order not to be confused with the local compiler, the name of the Cross Compiler generally has a prefix, such
U-GCC, iSCSI-XXXX-Linux-GNU-G ++, etc.
10. How to Use the Cross Compiler
The usage method is similar to the local GCC, but it is special that the compiler must use the-L and-I parameters to specify the spar
C system library and header file, cannot use local (x86)
).
Example:
Iscsi-XXXX-Linux-GNU-GCC test. C-L/path/to/sparclib-I/path/to/sparcinclude
1. C/C ++ compiler included in GCC
GCC, CC, C ++, G ++, GCC and CC are the same. c ++ and G ++ are the same.
(:)) C Programs are compiled using gcc, and C ++ programs are compiled using G ++.
2. Basic GCC usage
GCC test. C will compile a program named A. Out.
GCC test. C-O test will compile a program named test. The-O parameter is used to specify the name of the generated program.
Word
3. Why does the undefined reference to 'xxxxx' error occur?
First, this is a link error, not a compilation error. That is to say, if only this error occurs, it indicates your program source code.
There is no problem. It is because the parameters are incorrect when you compile with the compiler. You didn't
A library is required for a specified link program. For example, if some mathematical functions are used in your program, you need to compile
Specify the program in the parameter to link to the math library by adding-lM to the compilation command line.
4. -L 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 component names?
Taking the math library for example, his library name is m and his library file name is libm. so. It is easy to see that the library file name
The Library name is removed from the header lib and tail. So.
Now we know how to get the library name. For example, we need to use a third-party library named Lib.
Test. So, we only need to copy libtest. So to/usr/lib.
When the-ltest parameter is added during compilation, we can use the libtest. So Library (of course, we must use the libtest. So library ).
Function, we also need header files matching libtest. So ).
Put in/LibAnd/Usr/libAnd/Usr/local/libThe Library in can be linked directly with the-l parameter, but if the library file
Not in these three directories, but in other directories.
If you only use the-l parameter, the link will still fail. The error message is probably "/usr/bin/ld: cannot find ".
-LXXX ", that is, the linked program LD cannot be found in the three directories.
Libxxx. So, then another parameter-l will be used, such as the commonly used X11 library, which is put in/usr/x11r
6. In the lib directory, we need to use-L/usr/x11r6/lib-
Lx11 parameter. The-l parameter is followed by the Directory Name of the library file. For example, we put libtest. So in/AAA/BB.
In the B/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. For example, libm. So, it is linked to/lib/libm. S.
O. X,/lib/libm. so.6 and link to/lib/libm-2.3.2.so,
If there is no such link, there will still be an error, because LD will only find libxxxx. So, if you want to use xxxx
Library, while only libxxxx. So. X or libxxxx-x.x.x.so, do
Link to the Ln-s libxxxx-x.x.x.so libxxxx. So
It is always troublesome to manually write the link parameters. Fortunately, many libraries provide programs that generate the link parameters.
Usually XXXX-config, which is stored 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 ", which is the G required to compile a gtk1.2 Program
TK Link parameter. In addition to the -- libs parameter, XXX-config also has a parameter.
Number is -- cflags used to generate the header
Contains the directory, that is, the-I parameter, which will be discussed below. You can try to execute GTK-config
-- Libs -- cflags to check the output result.
Now the question is how to use these output results. The most stupid way is to copy, paste, and copy the results.
Add 'xxxx-config -- Libs -- in the compilation command line --
Cflags '. For example, compile a GTK program: GCC gtktest. c 'gtk-config -- Libs -- cflags '.
Poor
Not much. Note that 'is not a single quotation mark, but the key on the left of the 1 key.
In addition to XXX-config, the new development kit usually uses PKG-config to generate link parameters.
Similar to XXX-config, XXX-config is a specific development kit.
But PKG-config contains the generation of many development kit link parameters. You can use the PKG-config -- list-all command
List all supported sdks. The usage of PKG-config is PKG.
-Config pagname -- Libs -- cflags, where pagname is the package name and PKG-config -- list-all
List one of the list. For example, gtk1.2 is named GTK +, PKG-
The role of config GTK + -- Libs -- cflags is the same as that of GTK-config -- Libs -- cflags. For example:
GCC gtktest. c 'pkg-config GTK + -- Libs -- cflags'
.
5. -Include and-I Parameters
-Include is used to include header files, but generally including header files are implemented in the source code using # include xxxxxx.
The-include parameter is rarely used. -The I parameter is used to specify the header file directory.
, The/usr/include directory is generally not specified, GCC knows to find it, but if the header file is not in/usr/I
We need to use the-I parameter to specify the nclude, such as the header file
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
An error "XXXX. h: no such file or directory" occurs. -I
You can use relative paths for parameters. For example, if the header file is in the current directory, you can use-I. to specify the parameters. What we mentioned above -- cf
The lags parameter is used to generate the-I parameter.
6. -O Parameters
This is a program optimization Parameter. Generally,-O2 is used to optimize the program, such as GCC test. C-O2.
The resulting programs are smaller than those not optimized, and the execution speed may also be improved.
High (I have not tested ).
7. -Shared Parameters
Used to compile dynamic libraries, such as gcc-shared test. C-o libtest. So.
8. Several related environment variables
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, which defines development.
Package installation path, Libs parameters, and cflags parameters.
Cc: used to specify the C compiler.
Cxx: used to specify the cxx compiler.
Libs: similar to 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.
Do not worry.
Environment variable setting method: Export env_name = xxxxxxxxxxxxxxxxx
9. Cross-Compilation
In general, cross-compilation is to compile and compile on one platform that can run on another platform with different architectures.
For example, on our local PC platform (x86 CPU), compile and compile
For programs on the CPU platform, compiled programs cannot run on the x86 CPU platform and must be placed in
CPU platform.
Of course, both platforms use Linux.
This method is widely used in different platform transplantation and embedded development.
Compared with cross-compilation, we usually compile locally, that is, compile on the current platform.
The program is also executed locally.
The compiler used to compile such a program is called the cross compiler. The Compiler used for local compilation is called the local compiler.
Generally, GCC is used, but this GCC is used with the local GCC compiler.
It is different. You need to use a specific configure parameter during GCC compilation to obtain GCC that supports cross-compilation.
In order not to be confused with the local compiler, the name of the Cross Compiler generally has a prefix, such
U-GCC, iSCSI-XXXX-Linux-GNU-G ++, etc.
10. How to Use the Cross Compiler
The usage method is similar to the local GCC, but it is special that the compiler must use the-L and-I parameters to specify the spar
C system library and header file, cannot use local (x86)
).
Example:
Iscsi-XXXX-Linux-GNU-GCC test. C-L/path/to/sparclib-I/path/to/sparcinclude