Method used by gcc (zhuan)

Source: Internet
Author: User
Tags mathematical functions
The method used by gcc (zhuan)-General Linux technology-Linux programming and kernel information. The following is a detailed description. 1. C/c ++ compiler included in gcc
Gcc, cc, c ++, g ++, gcc and cc are the same, c ++ and g ++ are the same, (I didn't understand what the previous half sentence meant) c Programs are compiled using gcc, and c ++ programs are compiled using g ++.

Translation

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 this error is the only one, it means that the source code of your program is correct. It means that the parameters used during compiler compilation are incorrect. You didn't

You need to use a library for a specified link program. For example, if some mathematical functions are used in your program, you need to specify the program to be linked to the math library in the compilation parameters by compiling the command line.

Add-lm.

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 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 library name. For example, if we want to use a third-party library named libtest. so, we just 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 ).

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 in other directories, then we

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 link program ld cannot be found in the three directories.

Libxxx. so, then another parameter-L is used. For example, the common X11 library is stored in the/usr/X11R6/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, 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, such as libm. so it links to/lib/libm. so. x,/lib/libm. so.6 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 the xxxx library, and 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 library sdks provide programs that generate the link parameters. The name is usually xxxx-config, which is usually placed 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 ": This is the gtk Link parameter required to compile a gtk1.2 program. xxx-config has a parameter besides the -- libs parameter.

Number is -- cflags used to generate the header
Contains the directory, that is, the-I parameter, which will be discussed below. Run 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, or copy. The clever way is to add this 'xxxx-config -- libs -- in the compilation command line --

Cflags '. For example, compile a gtk program: gcc gtktest. c 'gtk-config -- libs -- cflags '.
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. The usage is similar to xxx-config, But 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 to list all supported development kits. The usage of pkg-config is pkg.

-Config pagName -- libs -- cflags, where pagName is the package name and one of the lists listed in pkg-config -- list-all. For example, the name of gtk1.2 is 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. Generally, the header files contained in the source code are implemented using # include xxxxxx, and 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/include, we will use the-I parameter to specify it, such as the header file

In the/myinclude directory, add the-I/myinclude parameter to the command line for compiling. If you do not add the parameter, you will get an error "xxxx. h: No such file or directory. -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. The -- cflags parameter we mentioned above is used to generate the-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 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/pkgconfig. 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. They are sometimes used for configure.
Environment variable setting method: export ENV_NAME = xxxxxxxxxxxxxxxxx

9. Cross-Compilation
Cross-compilation is commonly used to compile and compile a different platform that can run on different architectures on one platform, for example, on our local PC platform (X86 CPU) compiled to run on

For programs on the CPU platform, the compiled programs cannot run on the X86 CPU platform, and must be placed on the iSCSI CPU platform to run.
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 a cross compiler. In contrast, the compiler used for local compilation is called a 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

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/sparcInclude

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.