Linux gcc concepts and Parameters

Source: Internet
Author: User
Tags gtk

After studying linux gcc for a long time, I found that it is also necessary to understand linux gcc. Today I have collected the concepts and functions of Block devices for you. I hope you will have a lot to learn.

1. c/c ++ compiler included in linux gcc
Gcc, cc, c ++, g ++:
Gcc and cc are the same. c ++ and g ++ are the same. Generally, c Programs are compiled using gcc, and c ++ programs are compiled using g ++.

2. Basic usage of linux gcc
Gcc test. c will compile a program named a. out.
Gcc test. c-o test will compile a program named test.
-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 compilation are incorrect, you didn't specify the library to be used by the linked 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, the method is to add-lm # P # To the compilation 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 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 header lib and tail. so is the database name. Now we know how to get the database name. When we need to use the library name libtest provided by a third party. so, we just need to put libtest. so copy to/usr/lib, add the-ltest parameter during compilation, and we can use libtest. the so library must use libtest. so library function, we also need to work with libtest. so header files) in the/lib and/usr/local/lib libraries, you can directly use the-l parameter to link them, however, if the library file is not placed in these three directories but in other directories, the link will still fail if we only use the-l parameter. 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, it In the/usr/X11R6/lib directory, we need to use the-L/usr/X11R6/lib-lX11 parameter during compilation, and 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 is linked to/lib/libm. s.
O. x,/lib/libm. so.6 link to/lib/libm-2.3.2.so, if there is no such link, still will error, because ld will only find libxxxx. so, so if you want to use xxxx
Library, but only libxxxx. so. x or libxxxx-x.x.x.so, do a link on 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 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. In addition to the -- libs parameter, xxx-config also has a parameter -- cflags used to generate a header file containing the directory, that is, the-I parameter, we will talk about it 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, or copy the results. The clever way is to add the 'xxxx-config -- libs -- cflags 'in the compilation command line ', 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, the new development kit usually uses pkg-config to generate link parameters. The usage is similar to xxx-config, But xxx-config is for a specific development kit, however, 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 is one of the lists listed in pkg-config -- list-all, for example, the name of gtk1.2 is gtk +, and the function of pkg-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. Generally, the/usr/include directory does not need to be specified. gcc knows where to find it, however, if the header file is not in/usr/include, We need to specify it with the-I parameter. For example, if the header file is placed 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 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 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 also be improved.

7. shared Parameters
Used to compile dynamic libraries, such as gcc-shared test. c-o libtest. so # P #

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 the above-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.
Environment variable setting method: export ENV_NAME = xxxxxxxxxxxxxxx

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, 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 for porting different platforms and embedded development. Cross-compilation is usually called local compilation, that is, the program compiled on the current platform is also executed locally. The Compiler used to compile such programs is called the cross compiler. In contrast, the compiler used for local compilation is called the local compiler, generally, gcc is used, but this gcc is different from the local gcc compiler, you need to use specific configure parameters when compiling gcc to obtain the 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, for example

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) library header files can sometimes be used locally)
For example, a linux-gnu-gcc test. c-L/path/to/sparcLib-I/path/to/sparcInclude

Gcc and g ++
GCC is the most important software development tool in Linux. GCC is the gnu c and C ++ compilers. In fact, GCC can compile three languages: C, C ++, and Object CC ). The gcc command can be used to compile and connect the C and C ++ source programs at the same time.
GCC can be used to compile both C and C ++ programs. Generally, the C compiler uses the extension of the source file to determine whether it is a C program or a C ++ program. In Linux, the extension of the C source file is. c, and the extension of the C ++ source file is. C or. cpp.
The gcc command can only compile the C ++ source file, but cannot automatically connect to the library used by the C ++ program. Therefore, the g ++ command is usually used to compile and connect the C ++ program. The program automatically calls gcc for compilation.

Option description
-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.
-Msung optimizes code for 486.
-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 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.

The concepts and parameters of linux gcc are described above.

  1. How to become a Linux kernel developer
  2. What should I do if linux crashes?
  3. Linux application: Install operabrowser under Fedora
  4. Linux has nearly half of the number of server operating systems.
  5. Microsoft acknowledges the Linux status. We have not underestimated netbooks.

Related Article

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.