1. First Look at the General Liunx C program specific compilation process
Refer to the figure on the website http://blog.csdn.net/gengyichao/article/details/6544266
Converting a. c file (source code) that you write into a program that can run on hardware (executable code) requires the two main phases of the compile phase and link:
1) The compile stage first compiles a. c file into a. s assembly file through the "compiler", and then compiles the assembly code of this. s into a. O target file by "assembler";
Note: The pre-processed files for a series of lexical analysis, grammar analysis, semantic analysis and optimization resulting in the corresponding assembly code files, is the core part of the program construction, but also one of the most complex part. Assembly: The Assembly code according to the instruction table to the machine can execute instructions, a compilation of the general statement corresponding to a machine instruction;
2) "linker" generates an executable by connecting the. O Target code in the other. O Library files and 1.
The file stream by the three kinds of "machine" processing, respectively, showing four forms, can be summarized as three "four" files ", this is the C program compiling and linking process.
In particular, the compiler's process of compiling the source file into a compilation file is divided into: The preprocessing phase processes pre-compiled instructions that begin with "#" in the source code, such as "#include", "#define" (Generate. I code files) and optimization stages many ides and compilers merge compiled and linked processes, called build
- 2. next look at the specific file names that are involved in compiling and connecting
Basic files
C source code without pre-treatment
. h C Header File
. I pre-processed C source code
. s generated assembly-language code
. o The target file generated after compilation, equivalent to the. obj file in Windows
Library files
. A is a static library file
. So is a shared library
Explanation: *.c generally make our own editing of the code, so that the crystallization of our labor; *.h is generally our hand-generated interface file, if you wish, you can also use the GCC option-aux-info to generate the *.c after completion, and *.I is a pre-processed build file, is a file automatically generated by GCC under option-e compilation; *.O is the target file generated after the compilation, and *.s is the assembly language code generated by GCC in the option-s compilation, for high performance programs can first generate assembly language files and optimize the assembly, and then use the optimized assembly to generate the target file and link
Example of the GCC generation process of several files using SERVER.C as an example
Generate SERVER.I
$ gcc-e Server.c-o server.i
Generate assembly language Files Server.s
$ gcc-s Server.i-o SERVER.S
Generate the target file server.o
$ gcc-c Server.i
$ gcc-c SERVER.S
To build an executable file
$ gcc-o Server server.o
Operation and Results
$./server
Directly generated
$ gcc-c SERVER.C
3. Introduction to library files that are very important in Liunx programming
Sometimes it is necessary to compile a set of code into a library, which is used in many projects, for example, libc is such a library, we will use in different programs in the LIBC library functions (such as printf), but also use the variables in the libc (such as the Environ variable to be talked about later). Using GNU Tools How do we create our own library of programs under Linux, a "program function library" simply means that a file contains compiled code and data that can be used by other programs afterwards. The Library of program functions makes the whole program more modular, easier to recompile, and more convenient to upgrade. The Library of program functions can be divided into 3 types: static function library (libraries), shared function library (GKFX libraries), and dynamic load function library (dynamically loaded libraries).
1) Shared Library
. So is a shared library, full name Sharedobject, for dynamically connected, and DLL almost shared library with. So end. (so = = share object) does not use the code of a function in a copy of the program, as in the case of a static library, but only as a token. Then dynamically load the required modules when the program starts to run. Therefore, the application still needs the support of the shared library when it is running. A shared library is a much smaller file than a static library.
. LA for Libtool auto-generated some shared libraries, vi edit view, mainly recorded some configuration information.
If you are writing a shared library that supports all the useful features, you must follow a series of conventions in the process of writing. You must understand the differences between the different names of the library, such as the difference between its "soname" and "real name" and how they interact. You also need to know where you should put these library functions in your filesystem and so on. Let's look at these issues in detail below. Each shared function library has a special name, called "Soname". The soname name must be prefixed with "Lib", followed by the name of the library, then ". So", and finally the version number information. One exception, however, is that the very bottom C library functions are not named after Lib. Each shared function library has a real name ("real name"), which is the file containing the real library function code. The real name has a major version number, and a release version number. The last release version number is optional and can not be used. The major version number and release number allow you to know exactly what version of the library function you are installing.
Another name is the name of the library that the compiler needs to compile, which is a simple soname name and does not contain any version number information.
2) Static Library
. A is a static library file that represents archive, which is good for multiple. O target files are combined for static connections, and static libraries are used when the program is linked, and the linker copies the code from the library to the application using the functions in the program. Once the link is complete, the static library is not required to execute the program. Because each application that uses a static library needs to copy the code of the function used, the statically linked file is larger.
The static function library is actually a simple set of common target files, which is generally used as a suffix of ". A" as a file. You can use the AR program to generate a static function library file. AR is the abbreviation of archiver. The static library is no longer used as much as it used to be, mainly because the shared library has a lot of advantages over it. Slowly, everyone likes to use the shared function library. However, in some places the static function library is still in use, one to maintain some of the previous program compatibility, and secondly, it is relatively simple to describe. The static library function allows the programmer to link the program without recompiling the code, saving the time to recompile the code. However, in the face of such a fast computer today, the general program of the recompile will not take much time, so this advantage is not as obvious as it used to be. The static library is useful for developers, for example, if you want to use the functions you provide to others, but you want to keep the source code of the function secret, you can provide a static library file for others. In theory, the code generated using the ELF-formatted static library function can run faster than a program that uses a shared library of functions (or a dynamic library of functions), presumably 1-5%. To create a static function library file, or to add a new target code to an existing static function library file, you can use the following command:
AR RCS my_library.a file1.o file2.o
Once you have created a static function library, you can use it. You can use it as part of your compilation and connection process to generate your executable code. If you compile the executable code with GCC, you can specify the library function with the "-l" parameter. You can also use LD to do this, using its "-L" and "-l" parameter options.
3) Dynamic load library (dynamically loaded (DL) libraries)
A dynamically loaded library (dynamically loaded (DL) libraries) is a library of functions that can be loaded during a program's run. Instead of loading as a shared library when the program starts. DL is useful for implementing plug-ins and modules, because they allow the program to wait for the plug-in to be loaded when allowed. In Linux, the file format of a dynamic library is no different from a shared library, the main difference being that the shared library is run-time loaded.
The static function library is added to the target program before the program executes, while the shared function library is loaded into the program when the program starts, it can be shared by different programs, and the dynamic loading function library can be loaded dynamically at any time when the program is running. In fact, the dynamic function library is not another library function format, but the difference is how the dynamic loading function library is used by programmers. We'll give you an example later.
4 . Finally, a grilled "undefined reference to" appears when using "Yar.h"
First, the code is simple enough to contain the "Yar.h" header file
GCC Compilation connection Error
Obviously, Yar.h does not link to other. c files, look at the Yar.h
Using commands
-L. Indicates that the current directory is joined to the library search path. The default library search path is in the/usr/lib directory. Also here is the easy-to-confuse parameter-I, which represents the path to the search header file. In this way, when looking for a header file, GCC will first go to the directory specified by-I and then the system default directory.
-L Parameter:-lname represents the Libname.a or libname.so file under the Library search directory, which is one of the reasons why the library file starts with Lib. a convention. Of course, if your library file is not libserver, it's server. Then you cannot compile with the-l parameter.
Ok, Fix it, no error occurred
Reference:
- Http://hahack.com/wiki/c-linker.html
- Http://bbs.chinaunix.net/thread-2037617-1-1.html
- http://laokaddk.blog.51cto.com/368606/489821
LIUNX Program compilation process introduction and Bug Resolution