Using GCC to compile C and C + + under Linux

Source: Internet
Author: User

GCC-------> Default compile C file (gcc xxx-lstdc++ link to C + + library compilation c++,g++ default compile C + +)

A The CC compiler process is divided into four stages
Pretreatment (pre-processing)
Compiling (compiling)
Compilation (assembling)
Links (linking)

1.1 pretreatment (pre-processing)

GCC-E test.c-o test.i or GCC-E test.c

You can output the code in the Test.i file that contains the test.c after preprocessing. Open the Test.i file and take a look, and you'll see. The subsequent instruction is to output the preprocessed code directly in the command-line window.

The-e option of GCC allows the compiler to stop after preprocessing and output the preprocessing results. The preprocessing result is to insert the contents of a file such as # include into the current file.

1.2 Compiling (compiling)

After preprocessing, you can compile the generated test.i file directly and generate the assembly code:

Gcc-s Test.i-o Test.s

The-s option of GCC, which indicates that the assembly code is stopped and the-O output assembly code file is generated during program compilation.

1.3 Assembly (assembling)

For the generated assembly code file, the Test.s,gas assembler is responsible for compiling it as a target file, as follows:

Gcc-c Test.s-o TEST.O

1.4 Links (linking)

The GCC connector is provided by gas and is responsible for connecting the program's target file with all the additional target files needed to eventually generate the executable file. Additional target files include a static connection library and a dynamic connection library.

For the TEST.O generated in the previous section, connect it to the C standard input and output library, and the resulting program test

GCC Test.o-o Test

For this program, the one Step compiler command is:

GCC Test.c-o Test

II: library File links

When developing software, it is relatively uncommon to not use a third-party library at all, usually with the support of many libraries to complete the function. From the programmer's point of view, the library is actually a collection of header files (. h) and library files (so, or LIB, DLLs). Although most of the functions under Linux are placed in the/usr/include/directory by default, and the library files are placed in the/usr/lib/directory, the library files used by Windows are mainly placed in the include and Lib under the directory of Visual Stido. and the System folder. But there are times when we need to use a library that is no longer in these directories, so GCC must use its own method to find the required header and library files at compile time. (The path of the header file with-I, the library file path with-L, for example,-i/usr/myinclude-l/usr/mylib)

The-l parameter is used to specify the library to which the program is linked, 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, his library name is M, his library filename is libm.so, it is easy to see, the library file name of the head Lib and tail. So removed is the library name (the function library named "Lib" is the UNIX convention, ". So" is the standard extension of the shared vault)

2.1 Compiling into an executable file

First we want to compile test.c as the target file, this time need to execute

Gcc–c–i/usr/dev/mysql/include Test.c–o TEST.O

2.2 Links

Finally, we link all the target files to the executable file:

Gcc–l/usr/dev/mysql/lib–lmysqlclient test.o–o Test

The library files under Linux are divided into two categories: dynamic-link libraries (usually ending with. So) and static-link libraries (usually ending with. A), except that the code required to execute the program is dynamically loaded at run time, or statically at compile time.

2.3 Using static link libraries when linking is mandatory

By default, GCC takes precedence over dynamic-link libraries when linking, and only considers static-link libraries when the dynamic-link library does not exist, and, if needed, with the-static option at compile time, forcing static-link libraries to be used.

In order for GCC to use only static link libraries when linking, you can use the following command when you need the library files libmysqlclient.so and libmysqlclient.a in the/usr/dev/mysql/lib directory:

Gcc–l/usr/dev/mysql/lib–static–lmysqlclient test.o–o Test

Search path order when static library links:

1. LD will go to the parameters in the GCC command-l
2. Re-search for GCC environment variables Library_path
3. Find the default directory/lib/usr/lib/usr/local/lib This is the original compile GCC when written in the program

Dynamic Link-time, execution-time search path order:

1. The dynamic library search path specified when compiling the target code
2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path
3. The dynamic library search path specified in configuration file/etc/ld.so.conf
4. Default dynamic library search path/lib
5. Default dynamic Library search path/usr/lib

About environment variables:
Library_path environment variable: Specifies the program static link library file search path
LD_LIBRARY_PATH environment variable: Specifies the program dynamic link library file search path

Summary: The source file first generates an intermediate target file, which is then generated by the intermediate target file. At compile time, the compiler detects only the program syntax, and whether the function or variable is declared. If the function is not declared, the compiler will give you a warning, but you can generate an object File. When linking the program, the linker will find the implementation of the function in all the object file, if not found, then the link error code will be reported

Three: Makefile Tools

Make is a command tool, a command tool that interprets instructions in Makefile, and makes a file named "Makefile" or "Makefile" in the current directory. Make makes a layer-by-layer look at the dependencies of the file (if the process file is newer than the generated target file and needs to be recompiled) until the first target file is eventually compiled.

The rules of Makefile

Target ...: Prerequisites ...
Command
...
...

Target this one or more of the destination files depend on the files in prerequisites, whose generation rules are defined in the command. White point is that if more than one file in the prerequisites is newer than the target file, command-defined commands are executed. This is the rule of makefile. This is the core content of makefile.

3.1 Basic Makefile

    TARGET:PREREQUISITES1.O prerequisites2.o prerequisites3.o/                -o target PREREQUISITES1.O PREREQUISITES2.O prerequisites3.o display.o     prerequisites1.o:prerequisites1.c prerequisites1.h            -  C main.c    prerequisites2.o:prerequisites1.c prerequisites2.h            -c prerequisites2.c    PREREQUISITES3.O:PREREQUISITES3.C prerequisites3.h            -C prerequisites3.c Clean    :             RM Target PREREQUISITES1.O prerequisites2.o prerequisites3.o
View Code

The backslash (/) is the meaning of the line break. In this makefile, the target file contains the intermediate target file (*.O), and the dependent file (prerequisites) is the. c file and the. h file after the colon. Each of the. o files has a set of dependent files, and these. o files are also dependent files that execute the file target. Dependency is essentially a description of which files are generated by the target file.

After defining the dependencies, the subsequent line defines how to generate the operating system commands for the target file, so be sure to start with a TAB key . Make and no matter how the command works, he just executes the defined command. Make compares the modified date of the targets file and the prerequisites file, and if the date of the prerequisites file is newer than the date of the targets file, or target does not exist, then make executes the subsequent defined command.

3.2 Variable Makefile

The previous example saw that the string of [. O] Files was repeated two times when the target file was compiled, and if our project needs to add a new [. o] File, then we need to add in two places (should be three places and a place in clean). We declare a variable, obj, equal to the dependent [. O] File, and then use this variable in our makefile in "$ (objects)" mode

3.3 Make automatic derivation

GNU make is powerful, it can automatically deduce the file and the command behind the file dependencies, so we don't have to write a similar command after each [. o] File. Make sees a [. o] File, it automatically adds the [. c] File to the dependency, and if makes finds a prerequisite1.o, then prerequisite1.c is the prerequisite1.o dependent file. And Cc-c prerequisite1.c will also be deduced. The following code:

obj = prerequisites1.o prerequisites2.o prerequisites3.otarget: $ (obj)/                -o target $ (obj)    prerequisites1.o:prerequisites1.h    prerequisites2.o:prerequisites2.h    prerequisites3.o: Prerequisites3.h        . Phony:clean clean    :             RM target $ (OBJ)
View Code

This method, the "cryptic rule" of make. The contents of the above file, ". Phony "means that clean is a pseudo-target file.

Using GCC to compile C and C + + under Linux

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.