Both gcc and g ++ are GNU compilers.
But there are some differences between the two: The. c file gcc treats it as a c program, and g ++ as a c ++ program. Both the suffixes are c ++ programs. For cpp programs, both gcc and g ++ are the same in the compilation phase and are compiled using gcc. However, in the Link phase, gcc cannot automatically connect to the library used by the c ++ program, the format is gcc helloworld. cpp-lstdc ++-o helloworld
Therefore, we usually use g ++ for connection (g ++ will automatically connect to common c ++ libraries ), therefore, for ease of use, g ++ is used to compile all links of cpp programs. This gives us the illusion that cpp can only be compiled using g ++.
What is the Gcc compilation process?
The following uses the helloworld program as an example to explain the process:
The content of hello. c is as follows:
# Include <stdio. h>
Int main ()
{
Printf ("hello world \ n ");
}
Step 1: preprocessing
The Preprocessing process is as follows. The preprocessing stage processes macro and include commands in the Code and performs syntax checks. The command for this process is: # gcc-E hello. c-o hello. I execute this part to generate a hello. I file, as shown below:
We can see that the files inside include are replaced by preprocessing, and the results file after preprocessing is very large. Therefore, it is best not to introduce unused header files in future programs, this will reduce the processing time and space.
Step 2: The assembler generates the Assembly Code
This step is to compile the pre-processing file and generate the assembler. The command is as follows:
We can see that 59 lines of assembler are generated.
Step 3: switch from assembler to intermediate target file
This step further processes the compiled code. Each source program generates the corresponding target file, which is a file with the. o Extension. The command is as follows:
Step 4: connect to the target file and generate executable programs
This phase is called the link phase. What this phase completes is to connect the target file to generate the corresponding final target file (Executable File, static library, or dynamic library)
Now, the executable file in this example generates hello, and the run is:
How to Use g ++ to compile dynamic and static libraries? How can I use g ++ to connect non-standard libraries and applications?
What is a database? Simply put, a library is a set of functions and variables that have been written and compiled code. It is designed to improve development efficiency and operation efficiency. Libraries can be divided into static libraries and dynamic libraries (shared libraries). in linux, the extension of static libraries is. a, and the extension of dynamic libraries is. so.
A static library copies the library in the target program when each program is linked. When the target program is generated, the program can run independently from the library file, in other words, the original file will still work normally even if the program is deleted.
The shared library can be shared by multiple applications and dynamically loaded when the program is running. Therefore, even if a shared library is no longer used for each application, it should not be deleted, because other reference programs may need this library.
1. How to generate a database
The following shows how to generate static and dynamic libraries:
The process of generating a static library is to compile each original file to generate an intermediate target file, then use the package program to package the program once, and finally generate a static library file. The following example illustrates the problem:
The directory structure is
Contains 3 files, including hello. c is the source file used to generate the static library, hello. h is the header file, test. c is the test program used to test whether the library is generated correctly. In this example, we will set hello. c generate libhello. a, and then use test. cpp connects, and finally generates an executable hello file.
First, generate the library file,
Then, link the static library file to test. c and libhello. a to generate the corresponding application testhello.
In this example, after we delete the static library, the program still runs normally.
The following command is used to generate a dynamic library to connect to an application.
Gcc-shared-fPIC hello. c-o libhello. so, a dynamic library is generated.
Add the so path to the environment variable. Note that the path should be added to LD_LIBRARY_PATH. In addition, run the ldconfig command, which is in the/sbin/directory. In this way, the instance runs normally. The following is a test:
If it is deleted, it will:
It can be seen from the above that it is more convenient to use gcc in a small project. If you want to use another large project, we must remember the gcc command. If you want the program to be transplanted, you must have enough documents to manage large projects. The following provides a solution to solve these problems through makefile. When you port your program, you only need to perform simple operations to generate a binary file, this simplifies the management of large projects.
Explanation of GCC compiler options
1. Basic options
-E is only used for preprocessing, without compilation, assembly, and connection.
-S is stopped after compilation and will be connected without any further changes
-C compilation or compilation of files without connection
-O file: Specifies the output file name.
2. Warning options
-Wall: Enable all warning information
-Werror: cancels the compilation operation in case of a warning. The warning is regarded as an error.
-W: Disable all warning information
3. Optimization Options
-O0: No optimization is performed.
-O or-O1: Performs basic optimization, which in most cases will make the program run faster.
-O2: In addition to completing-O1-level optimization, some other adjustments are required, such as processor command scheduling, which is only the default Optimization of GNU release software.
-O3: In addition to completing-O2-level optimization, it also performs loop expansion (which often increases the execution speed) and other pre-processor-related optimization work.
-OS: generates the smallest executable file, which is mainly used in the embedded field.
4. connector options
-Idirectory: Add a new directory to the GCC header file search path.
-Ldirectory: Add a row directory to the GCC library file search path.
-The llibrary prompts the connection program to include the specified library file when creating the executable file.
-Static force static library
-Shared libraries are forcibly used.
5. Other options
-Xlanguage: Specifies the programming language of the input file.
-V: displays the version number of the compiler.
-G: obtain detailed information about the debugging program.
-Ansi supports c Programs that comply with ansi standards.
Now that we have already talked about this much, let's talk about some environment variables used by gcc.
In addition to the well-known CFLAGS and CXXFLAGS (in fact, it is the environment variable of Autoconf), let's talk about it:
All PATH environment variables (except LD_RUN_PATH) are directory lists separated by colons.
C_INCLUDE_PATH is the environment variable used to compile the C program. It is used to find the header file.
Environment variables used when CPLUS_INCLUDE_PATH is used to compile the C ++ program, used to find the header file.
The environment variable used by OBJC_INCLUDE_PATH to compile the Obj-C program. It is used to find the header file.
Environment variables used by CPATH to compile C/C ++/Obj-C Programs, used to find header files.
COMPILER_PATH if you do not use GCC_EXEC_PREFIX to locate the subroutine, the compiler will look for its subroutine here.
The LIBRARY_PATH Connection Program searches for special connection program files in these directories.
LD_LIBRARY_PATH this environment variable does not affect the Compilation Program, but will affect the program running: The program searches for the directory list to find the shared library.
When the shared library cannot be found in the compiling program directory, the execution program must set this environment variable.
LD_RUN_PATH this environment variable does not affect the compilation program, but it may affect the program running: it indicates the name of the file at runtime, and the running program can get its symbolic name and address.
Because the address is not reloaded, it may apply the absolute address in other files. This is exactly the same as the "-R" option used by the ld tool.
The prefix of the name of all subprograms executed by the GCC_EXEC_PREFIX compiler program. The default value is "<prefix>/lib/gcc-lib /",
<Prefix> indicates the prefix specified by the configure script during installation.
LANG specifies the character set used by the compiler. It can be used to create wide character files, string texts, and comments. The default value is English. [Currently only supports Japanese "C-JIS, C-SJIS, C-EUCJP", does not support Chinese]
LC_ALL specifies the character classification of Multi-byte characters. It is mainly used to determine the character boundary of the string and the language used by the compiler to send diagnostic messages. The default setting is the same as that of LANG.
Several items related to Chinese: "zh_CN.GB2312, zh_CN.GB18030, zh_CN.GBK, zh_CN.UTF-8, zh_TW.BIG5"
The temporary directory where the TMPDIR Compilation Program stores temporary work files. These temporary files are usually deleted at the end of compilation.
I think it's better to learn from my end-to-end experience.