Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept of linux gcc and what are its parameters

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the concept of linux gcc and its parameters, the content is very detailed, interested friends can refer to, hope to be helpful to you.

The following is the editor to collect the concept and function of block equipment for you.

1. The linux gcc contains a cUniverse + compiler.

Gcc, cc, C++, Gmail:

Gcc is the same as cc. C++ and Gmail + are the same. Generally, c programs are compiled with gcc and C++ programs are compiled with Gmail +.

2. The basic usage of linux gcc

Gcc test.c will compile a program called a.out in this way

Gcc test.c-o test will compile a program called test.

The-o parameter is used to specify the name of the generator

3. Why does the undefined reference to 'xxxxx' error occur?

First of all, this is a link error, not a compilation error, that is to say, if there is only this error, there is nothing wrong with the source code of your program, it is that you used the compiler parameters incorrectly, and you did not specify the library to be used by the linked program. For example, if some mathematical functions are used in your program, you should specify that the program should link to the math library in the compilation parameters by adding-lm # P# to the compilation command line.

4, l parameter and L parameter

The-l parameter is used to specify the library to be linked by the program, and the-l parameter is followed by the library name, so what does the library name have to do with the real library file name? Take the math library for example. His library name is m and his library file name is libm.so. It is easy to see that removing the head lib and tail .so of the library file name is the library name. Well, now we know how to get the library name. When we want to use the library name libtest.so provided by a third party, then we just copy the libtest.so to / usr/lib and add the-ltest parameter when compiling. We can use the libtest.so library (of course, we need to use the functions in the libtest.so library, and we also need the header files that go with libtest.so). The libraries in / lib and / usr/lib and / usr/local/lib can be linked directly with the-l parameter, but if the library files are not placed in these three directories, but in other directories, if we only use the-l parameter, the link will still make an error. The error message is probably: "/ usr/bin/ld: cannot find-lxxx", that is, the linker ld cannot find libxxx.so in those three directories, then another parameter-L will come in handy, such as the commonly used X11 library, which is in the / usr/X11R6/lib directory, we will compile with the-L/usr/X11R6/lib-lX11 parameter, 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, which links to / lib/libm.s.

O.xMagne.com LBM. So.6 links to / lib/libm-2.3.2.so. If there is no such link, there will still be an error, because ld will only look for libxxxx.so, so if you want to use xxxx

Library, but only libxxxx.so.x or libxxxx-x.x.x.so, it is always troublesome to write link parameters manually with ln-s libxxxx-x.x.x.so libxxxx.so. Fortunately, many library development packages provide programs for generating link parameters, which are generally called xxxx-config and are generally placed in the / usr/bin directory. For example, the link parameter generator for gtk1.2 is gtk-config. Execute 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 ", which is the gtk link parameter needed to compile a gtk1.2 program. In addition to the-- libs parameter, xxx-config has another parameter-- cflags is used to generate the header file containing directory, that is, the-I parameter, which we will talk about below. You can try to execute gtk-config.

-- libs-- cflags, look at the output.

The problem now is how to use these output results. The stupidest way is to copy and paste or copy them. The smart way is to add the `output-- libs-- cuplos` to the compilation command line, such as compiling a gtk program: gcc gtktest.c `gtk-config-- libs-- cuplos`. That's enough. Note that `is not a single quotation mark, but the key to the left of the 1 key.

Except for xxx-config, new development packages now generally use pkg-config to generate link parameters, similar to xxx-config, but xxx-config is for specific development packages, but pkg-config contains the generation of link parameters for many development packages. You can list all supported development packages with the pkg-config-- list-all command. The usage of pkg-config is pkg-config pagName-- libs-- cflags, where pagName is the package name. It's one of the lists listed in pkg-config--list-all. For example, gtk1.2 's name is gtk+,pkg-config gtk+-- libs-- cflags has the same function as gtk-config-- libs-- cflags. For example: gcc gtktest.c `pkg-config gtk+-- libs-- crous`

5. Include and I parameters

-include is used to include header files, but generally, header files are implemented in # include xxxxxx in the source code, and the-include parameter is rarely used. -I parameter is used to specify the header file directory, / usr/include directory is generally not specified, gcc knows where to find, but if the header file is not in / usr/include, we have to use the-I parameter to specify, for example, the header file is placed in the / myinclude directory, then the compilation command line will be added with the-I/myinclude parameter, if you do not add you will get a "xxxx.h: No such file or directory" error. The-I parameter can use a relative path, for example, the header file is in the current directory, and you can use-I. To specify. The cflags parameter that we mentioned above is used to generate the-I parameter.

6. O parameter

This is a program optimization parameter. Generally,-O2 is used to optimize the program, such as gcc test.c-O2. The optimized program is smaller than the unoptimized one, and the execution speed may also be improved.

7. Shared parameters

Used when compiling dynamic libraries, such as gcc-shared test.c-o libtest.so # P#

8. Several related environmental variables

PKG_CONFIG_PATH: used to specify the path of the pc file used by pkg-config. By default, the / usr/lib/pkgconfig,pc file is a text file with a .pc extension, which defines the installation path of the development package, Libs parameters, Cflags parameters, and so on.

CC: used to specify the c compiler

CXX: used to specify the cxx compiler

LIBS: similar to the above-- libs.

CFLAGS: similar to the above-- cflags.

CC,CXX,LIBS,CFLAGS is generally not used when compiling manually, but is sometimes used when doing configure.

The setting method of environment variable is export ENV_NAME=xxxxxxxxxxxxxxxxx.

9. About cross-compilation

Generally speaking, cross-compilation is compiled on one platform that can run on another platform with different architectures.

If compiled on our PC platform (X86 CPU), it can run on sparc.

The program on the CPU platform, the compiled program can not run on the X86 CPU platform, it must be placed on the sparc CPU platform to run. Of course, both platforms use linux. This method is commonly used in cross-platform migration and embedded development. The compilation we usually do is called local compilation, that is, it is compiled on the current platform, and the compiled program is also executed locally. The compiler used to compile this kind of program is called cross compiler. Relatively speaking, what is used for local compilation is called local compiler. Gcc is generally used, but this kind of gcc is different from the local gcc compiler. You need to use specific configure parameters when compiling gcc to get gcc that supports cross-compilation. In order not to be confused with the local compiler, the names of cross-compilers generally have prefixes, such as sparc-xxxx-linux-gnu-gcc,sparc-xxxx-linux-gnu-g++ and so on.

10. How to use the cross compiler

The method of use is similar to that of local gcc, but there is one special thing: the compiler must use the-L and-I parameters to specify the compiler's sparc system libraries and header files, not local (X86) libraries (header files can sometimes be used locally)

Example: sparc-xxxx-linux-gnu-gcc test.c-L/path/to/sparcLib-I/path/to/sparcInclude

Gcc and Gmail +

The most important software development tool in Linux is GCC. GCC is GNU's C and C++ compiler. In fact, GCC can compile in three languages: C, C++, and Object C (an object-oriented extension of the C language). Using gcc command, you can compile and connect C and C++ source programs at the same time.

GCC can be used to compile both C programs and C++ programs. Generally speaking, the C compiler determines whether it is a C program or a C++ program by the suffix name of the source file. In Linux, the C source file has the suffix .c, while the C++ source file has the suffix .C or .cpp.

The gcc command can only compile C++ source files, not automatically connect to the libraries used by C++ programs. Therefore, the C++ + command is usually used to complete the compilation and connection of the C++ program, and the program will automatically call gcc to achieve compilation.

Option interpretation

-ansi only supports the ANSI standard C syntax. This option disables certain features of GNU C, such as asm or typeof keywords.

-c compiles and generates only the target file.

-DMACRO defines the MACRO macro as the string "1".

-DMACRO=DEFN defines MACRO macros with the string "DEFN".

-E runs only the C precompiler.

-g generates debugging information. The GNU debugger can take advantage of this information.

-IDIRECTORY specifies an additional header file search path DIRECTORY.

-LDIRECTORY specifies an additional function library search path, DIRECTORY.

Search for the specified function library LIBRARY when lLIBRARY is connected.

-m486 optimizes code for 486.

-o FILE generates the specified output file. Used when generating executable files.

-O0 does not optimize.

-O or-O1 optimizes the generated code.

-O2 is further optimized.

-O3 is further optimized than-O2, including the inline function.

-shared generates shared object files. It is usually used when building shared libraries.

-static prohibits the use of shared connections.

-UMACRO undefines MACRO macros.

-w does not generate any warning messages.

-Wall generates all warning messages.

About the concept of linux gcc and what the parameters are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report