1. C/C ++ compiler included in GCC
GCC, CC, C ++, G ++, GCC and CC are the same, and C ++ and G ++ are the same. CProgramIt is compiled with GCC, and the c ++ program is compiled with 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.
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 the program using the compiler. You didn't specify the library to be used by the linked program. For example, some mathematical functions are used in your program, then you need to specify the program link in the compilation parameters.
Math library by adding-lM to the compiling command line.
4.-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 as an example, his library name is m and his library file name is libm. so. It is easy to see that the library name is removed from the header lib and tail. So of the library file name.
Now we know how to get the database name. For example, we need to use a third-party library named
Libtest. So, we only need to copy libtest. So to/usr/lib, and add the-ltest parameter during compilation, we can use the libtest. So library.
(Of course, we need to use the libtest. So library function. We also need the header file supporting libtest. So ).
-L is directly used for libraries in/lib and/usr/local/lib.
The parameter can be linked, but if the library file is not placed in these three directories, but in other directories, then if we only use the-l parameter, the link will still fail, 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, such as the commonly used X11 library, which is placed in
In the/usr/x11r6/lib directory, we need to use-L/usr/x11r6/lib during compilation.
-Lx11 parameter. 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, for example
Libm. So it links to/lib/libm. So. X,/lib/libm. so.6 again link to/lib/libm-2.3.2.so if there is no such chain
An error occurs because LD only finds libxxxx. So. Therefore, if you want to use the XXXX library, only libxxxx. So. X or libxxxx-
X. So, do a link to 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 stored in the/usr/bin directory, for example, if the program for generating the Link parameter of gtk1.2 is GTK-config, run
GTK-config -- libs
The following output is displayed:
"-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.
Another parameter besides the -- libs parameter is -- cflags used to generate the header file containing the directory, that is, the-I parameter, which will be discussed below. You can try to execute GTK-config
-- Libs
-- Cflags: Check the output result. Now the question is how to use these output results. The most stupid way is to copy, paste, or copy. The clever way is to add this 'xxxx 'to the compilation command line.
-Config -- Libs -- cflags, 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.
In addition to XXX-config, New Development Kits are usually generated using PKG-config.
The usage of link parameters is similar to that of XXX-config, But xxx-config is for a specific development kit, but PKG-config contains the generation of many link parameters of the Development Kit,
You can use the PKG-config -- list-all command to list all supported development kits. 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 +,
The role of 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 generally including header files are used in the source code # I
Nclude
Xxxxxx implementation, 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,
If the header file is not in/usr/include, we need to use the-I parameter to specify it. For example, if the header file is placed in the/myinclude directory,-
I/myinclude parameter. 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-I
Parameter.
6.-O Parameters
This is a program optimization Parameter. In general,-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 (I have not tested it ).
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/pkgconfig. The PC file is a text file and the extension is. PC, which defines the installation path of the Development Kit, 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. They are sometimes used for configure.
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, compile programs that can run on the local PC platform (x86 CPU) and compile the programs on the x86
The CPU platform cannot run. It 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 call local compilation, that is, compiling on the current platform, and the compiled program is also executed locally.
The compiler used to compile such a program is called the cross compiler. In contrast, the compiler used for local compilation is called the local compiler. GCC is generally used, however, this GCC is different from the local GCC compiler. You must 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
10. How to Use the Cross Compiler
The usage method is similar to that of local GCC, but it is special that the-L and-I parameters must be used to specify the library and header file for the compiler to use the iSCSI system, rather than local (x86) ).
Example:
Iscsi-XXXX-Linux-GNU-GCC test. C-L/path/to/sparclib-I/path/to/sparcincludeAppendix: Main gcc/egcs options
-ANSI only supports the ANSI standard C syntax. This option will disable certain characteristics of gnu c, such as ASM or typeof keywords.
-C only compiles and generates the target file.
-Dmacro defines the macro with the string "1.
-Dmacro = defn: Define the macro with the string "defn.
-E only runs the C pre-compiler.
-G generates debugging information. The GNU Debugger can use this information.
-Idirectory: specify an additional header file to search for the path directory.
-Ldirectory: specify an additional function library to search for the path directory.
-Search for the specified library when connecting to llibrary.
-Mquota for 486CodeOptimized.
-O file: generate the specified output file. Used to generate executable files.
-O0 is not optimized.
-O or-O1 optimized code generation.
-O2 is further optimized.
-O3 is further optimized than-O2, including the inline function.
-Shared shared object generation. It is usually used to create a shared library.
-Static prohibit the use of shared connections.
-Umacro undefines macro macros.
-W does not generate any warning information.
-wall generates all warning information.