The method used gcc - Author: www.linuxfans.org mozilla
1. GCC includes C / C , G , GCC, and CCs are the same, C and G are the same, general C procedures compile with GCC, C program use G compile
2. GCC's basic usage GCC Test.c This will compile a program GCC Test.c -o test called a.out, which will compile a program called TEST, and -O parameter is used to specify the name of the generator.
3. Why do you show Undefined Reference to 'XXXXX' error? First this is a link error, not compiling errors, that is, if there is only this error, you don't have a problem with your program source itself, you use the compiler to compile the parameter is not right, you don't specify the link to use the library, For example, you have used some mathematical functions in your program, then you have to specify the program in the compilation parameter to link the math library, and the method is to join -LM in the compile command line.
4. -l parameter and -l parameter -L parameter is used to specify the library to be linked, the -l parameter is the library name, then what is the relationship between the library name and the true library name? Just take the mathematics library, his library name is M. His library name is libm.so, it is easy to see that the head lib and the tail of the library file name. SO is the library name.
Ok, now we know how to get the name. For example, we have to use a third-party library name called libtest.so, then we only need to copy libtest.so to / usr / lib, compile, compile - LTest Parameters, we can use the libtest.so library (of course, we need to use the function of libtest.so ", we also need the header file with libtest.so).
The library placed in / lib and / usr / lib and / usr / local / lib and can be linked directly with the -l parameter, but if the library file is not placed in these three directories, it is placed in other directories. At this time, we only use the -l parameter, the link will still be wrong, the error message is probably: "/ usr / bin / ld: cannot find -lxxx", that is, the linker LD can't find libxxx in that three directory. SO, then another parameter -L will send it, such as the common X11 library, put it in / usr / x11r6 / lib directory, we need to use -l / usr / x11r6 / lib -lx11 The parameter, the -l parameter follows the directory name where the library file is located. For example, we put libtest.so in / aaa / bb / ccc directory, the link parameter is -l / aaa / bbb / ccc -ltest
In addition, most libxxxx.so is just a link, with RH9 as an example, such as libm.so it is linked to /lib/libm.so.x ,/lib/libm.so.6 and link to /lib/libm-2.3. 2.so, if there is no such link, it will be wrong because LD will only look for libxxxx.so, so if you want to use the XXXX library, only libxxxx.so.x or libxxxx-xxxso, make a link can be LN -S libxxxx-xxxso libxxxx.so
Handmade is always very troublesome, but a lot of library development packs provide programs that generate link parameters. The name is generally called XXXX-Config, which is generally placed in / usr / bin directory, such as gtk1.2 link parameters generation The program is GTK-Config, which can get the following output "-L / usr / lib -l / usxt" -L / usr / lib -l / usxt "-L / usr / lib -l / usxt" -L / usr / lib -l / usxt) LX11 -LM, this is to compile a GTK link parameter required for a GTK1.2 program. XXX-Config has a parameter in addition to -libs parameters is --cflags used to generate the header file containing the directory, that is, -i Parameters, we will talk below. You can try GTK-Config --Libs --cflags to see the output results. The problem is how to use these outputs, the most benzene method is to copy the paste or copy, the smart method is to join this `xxxx-config --libs --cflags`, such as compiling a GTK program: GCC gtktest.c `gtk-config --libs --cflags is almost. Pay attention to `Not single quotes, but 1 button on the left. In addition to XXX-Config, now new development packs generally use pkg-config to generate link parameters, use the way to XXX-Config, but XXX-Config is a specific development package, but pkg-config contains many development packages. The generation of the link parameters, with the pkg-config --List-all command can list all the supported developments, the use of pkg-config is pkg-config pagname --libs --cflags, where PAGNAME is the name, which is PKG. -config - List of lists in List, such as GTK1.2's name is GTK , PKG-Config GTK --LIBS - CFLAGS's role is the same as gtk-config --libs --cflags. For example: GCC gtktest.c `pkg-config gtk --libs --cflags`.
5. -include and -i parameter -include is used to include header files, but in general, the header file is included in the source code with #include xxxxxx, and the -Include parameter is rare. The -i parameter is used to specify the header file directory, / usr / include directory is generally not specified, GCC knows to find it, but if the header file is not in / usr / include, we must use the -i parameter, such as head The file is placed in the / myinclude directory, and the compile command line will add -i / myinclude parameters, and if you don't add you, you will get a "xxxx.h: no so file or directory" error. -I parameters can be used with relative paths, such as header files in the current directory, can be specified in -i. The above -cflags parameter we mentioned above is used to generate the -i parameter.
6. -O parameter This is a program optimized parameter. Generally, -O2 is used to optimize the program, such as GCC Test.c -O2, optimized program is less than optimized, and the execution speed may also increase (I No test).
7. -Shared parameters To compile dynamic libraries, such as gcc -shared test.c -o libtest.so
8. Several related environment variables pkg_config_path: The path used to specify the PC file used by pkg-config, the default is / usr / lib / pkgconfig, the PC file is a text file, the extension is .PC, define the installation path of the development package , Libs parameters and cflags parameters, etc. CC: Used to specify a C compiler. CXX: Used to specify the CXX compiler. Libs: The effect of the above -LIBS is similar. Cflags: The effect of the above -cflags is similar. CC, CXX, Libs, CFLAGS are manually compiled, and sometimes it is sometimes used when configure is doing, in general. Environment Variable Setting Method: Export Env_name = XXXXXXXXXXXXXXXXXX9. About cross-compilation cross-compilation is popular in a platform that can be compiled on another platform that runs in the architecture in the architecture, such as compiling can run on the SPARC CPU platform on our PC platform (X86 CPU). The program, the compiled program is unable to run on the X86 CPU platform, and must be placed on the SPARC CPU platform. Of course, both platforms are Linux.
This approach is very common in the isometric transplant and embedded development.
Relative to cross-compilation, our usual compilation is called local compilation, that is, compiled in the current platform, the program is executed locally.
The compiler used to compile this program is called the cross-compiler. Relatively, it is used to make local compilers, which are generally GCC, but this GCC is not a local GCC compiler. The same, you need to support GCC supporting cross-compiled with a specific Configure parameter when compiling GCCs.
In order to confuse the local compiler, the name of the cross-compiler generally has a prefix, such as sparc-xxxx-linux-gnu-gcc, sparc-xxxx-linux-gnu-g , etc.
10. How to use the cross-compiler How to use the local GCC, but a special thing is that the compiler must specify the library and header file of the SPARC system with the -1 and -i parameters, and cannot use the local (x86) library (header) Files can sometimes be locally available. Example: sparc-xxxx-linux-gnu-gcc test.c -l / path / to / sparclib -i / path / to / sparclude