GCC command line detailed solution (transfer)

xiaoxiao2021-03-06  40

The method used gcc - Author: www.linuxfans.org mozilla

1. GCC includes C / C compiler GCC, CC, C , G , GCC and CCs are the same, C and G are the same, (did not see what the half sentence is meaningful :)) General C procedure GCC compiles, C programs use G

Translation

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, it is, you are using the compiler, you are not used.

There is a specified link program to use the library, such as using some mathematical functions in your program, then you have to specify the program in the compilation parameter to link the math library, the method is in the compile command line.

Add -LM.

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 to provide library name called libtest.so, then we only need to copy LibTest.so to / usr / lib

In the case of compiling the -ltest parameter, 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. That time we

Only the -l parameter, the link is still error, the error message is probably: "/ usr / bin / ld: cannot find -lxxx", that is, the link program LD can't find in the three directory

LIBXXX.SO, then another parameter -L will send it, such as the commonly used X11 library, it is placed in / usr / x11r6 / lib directory, we should use -l / usr / x11r6 / lib when we compile -

The LX11 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 still 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-x.x.x.so, do one

Ln -s libxxxx-x.x.x.so 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, and GTK-Config -Libs can be output "-L / usr / lib -l / usr / x11r6 / lib -lgtk -lgdk -rdynamic"

-lgmodule -lglib -ldl -lxi -lxext -lx11 -lm, this is the GTK link parameters required to compile a GTK1.2 program, and XXX-Config has a ginseng in addition to -LIBS parameters.

The number is -cflags to generate the header file containing the directory, that is, the -i parameter, we will talk below. You can try GTK-Config --Libs --cflags to see the output results. The current problem is how to use these outputs, the most stupid method is to copy the paste or copy, the smart method is to join this in the compile order `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, using methods similar to XXX-Config, but XXX-Config is for specific development packages

However, PKG-Config contains the generation of a lot of development packages, with the pkg-config --List-all command, which can list all the developments of the support, the use of pkg-config is PKG

-config PAGNAME --LIBS - CFLAGS, where PAGNAME is the name, which is one of the lists listed in the pkg-config - list-all, such as GTK1.2's name is GTK , PKG-

The role of Config gtk --libs --cflags 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. -I parameter is used to specify header file directories

The / usr / incrude directory is generally not specified, GCC knows to find it, but if the header is not in / usr / include, we must use the -i parameter, such as header file

In the / myinclude directory, the compile command line will be added to the -i / myinclude parameter, if you don't add you, you will get an error in "xxxx.h: no such file or directory". -I

The parameters can be used in 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, it is used to optimize the program, such as GCC Test.c -O2, and optimized the program is not optimized, and the speed may also be mentioned.

High (I have not tested).

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, defined inside

Mounting path, 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 = xxxxxxxxxxxxxxxx

9. About cross-compilation cross-compilation is popular in a platform to compile the architecture in another platform, such as compiling can run in SPARC on our PC platform (x86 CPU).

The program on the CPU platform, the compiled program cannot be 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 has a local GCC compiler.

Different, you need to support cross-compiled GCC 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

转载请注明原文地址:https://www.9cbs.com/read-66808.html

New Post(0)