This article was reproduced from: http://www.cnblogs.com/yaozhongxiao/archive/2012/03/16/2400473.html
If you want to reprint, please indicate the original source. Thank you.
----------------------------------------------------------------------------------------
parameters and commands commonly used in GCC1. Execution Although we call GCC a C compiler, the process of using GCC to generate executables from a C language source code file is not just a compilation process, but a four interrelated steps: preprocessing (also known as precompilation, preprocessing), compilation ( Compilation), assembly (Assembly), and links (linking). Command gcc
(1). First call the CPP for preprocessing, and during preprocessing, analyze the file inclusions (include), precompiled statements (such as macro definitions, define, etc.) in the source code file.
(2). Then call CC1 to compile, which generates a target file with an. o suffix based on the input file.
(3). The Assembly process is for assembly language steps, called as to work, generally speaking,. S is the suffix of the assembly language source code files and assembly,. s suffix assembly language files are precompiled and compiled to generate a target file with an. o suffix.
(4). When all the target files are generated, GCC invokes the LD to complete the final critical work, which is the connection. During the connection phase, all the target files are placed in the appropriate place in the executable program, and the library functions that the program calls to are also connected to the appropriate location from the respective archive.
Basic Concepts
The library has dynamic and static two kinds, the dynamic usually uses. So as the suffix, static with. A is the suffix. For example: Libhello.so libhello.a in order to use a different version of the library in the same system, you can add a version number suffix to the library file name, for example: libhello.so.1.0, because the program connection defaults to. So for the file suffix name. So in order to use these libraries, you typically use the way you create symbolic connections. Ln-s libhello.so.1.0 libhello.so.1ln-s libhello.so.1 libhello.so When you want to use a static library, the connector finds the functions required by the program, and then copies them to the execution file, because the copy is complete, So once the connection is successful, the static library is no longer needed. However, for dynamic libraries, this is not the case. The dynamic library leaves a tag inside the executor that indicates that the library must first be loaded when the program executes. Due to the space saving of the dynamic library, the default operation to connect under Linux is to connect the dynamic library first, that is, if both static and dynamic libraries exist and are not specifically specified, they will be connected to the dynamic library. Now suppose there is a program development package called Hello, which provides a static library libhello.a a dynamic library libhello.so, a header file Hello.h, a header file that provides SayHello () This function void SayHello (); There are also some explanatory documents. This typical program development package structure and dynamic Library connection to Linux the default is to connect with the dynamic library, the following program TESTLIB.C using the SayHello () function in the Hello Library
#include <> #include <>int main () {SayHello (); return 0;}
compile with the following command $GCC-C testlib.c-o testlib.o Connect with the following command: Note that LIBHELLO.O and LIBHELLO.A are assumed to be in the default library $GCC testlib.o-lhello-o testlib connection Search path under/usr/lib, if you want to add the-l parameter in other locations with the static library connection trouble, mainly is the parameter problem. Or the above example: $GCC testlib.o-o Testlib-wi,-bstatic-lhello Note: This particular "-wi,-bstatic" parameter is actually passed to the connector ld. Indicates that it is connected to a static library, which is of course not required if there is only a static library in the system. If you want to connect with more than one library, and each library is connected in a different way, such as the above program is both static connection with Libhello, but also dynamic connection with Libbye, its command should be: $GCC testlib.o-o testlib-wi,-bstatic- Lhello-wi,-bdynamic-lbye
--------------------------------------------------------------------------------
2, the path of the dynamic library in order to let the execution of the program successfully found dynamic library, there are three ways:(1) Copy the library to the/usr/lib and/lib directories. (2) Add the path of the library to the LD_LIBRARY_PATH environment variable. For example, dynamic library libhello.so in the/home/ting/lib directory, bash for example, using the command: $export ld_library_path= $LD _library_path:/home/ting/lib (3) Modify/ etc/ld.so.conf file, add the path where the library is located to the end of the file, and perform a ldconfig refresh. In this way, all library files in the joined directory are visible.
--------------------------------------------------------------------------------
3. View symbols in the librarySometimes you might want to see what functions are in a library, and the NM command prints out all the symbols involved in the library. A library can be either static or dynamic. NM lists a number of symbols, there are three common: one is called in the library, but is not defined in the library (indicating the need for additional library support), with u, a function defined in the library, denoted by T, this is the most common, the other is the so-called "weak state" symbol, although they are defined in the library, However, it may be overridden by a symbol with the same name in another library, denoted by W. For example, suppose the developer wants to know if printf () is defined in the Hello Library mentioned above: $nm libhello.so |grep printf u where printf u means that the symbol printf is referenced, but not defined within the function, which can be inferred, To use the Hello Library normally, you must have additional library support, and then use the LDD command to see which libraries Hello depends on: $ldd Hello libc.so.6=>/lib/libc.so.6 (0x400la000)/lib/ld-linux.so.2 =>/lib/ld-linux.so.2 (0x40000000) from the above results can continue to see where printf ends up being defined, interested in go on
--------------------------------------------------------------------------------
4. Build LibraryThe first step is to Yi the source code into the target code. Take the following code, for example, to generate the Hello library used above:
#include <> void SayHello () {printf ("Hello,world");}
Yi the file with gcc, Yi can use any of the full method of the Yi parameters, such as-G add debugging code, such as: Gcc-c hello.c-o hello.o (1) connected to a static library connected to a static library using AR command, in fact, AR is the meaning of archive $ar CQS LIBHELLO.A hello.o (2) Connect to a dynamic library to generate a dynamic library with GCC, because there may be multiple versions, you typically specify the version number: $GCC-shared-wl,-soname,libhello.so.1-o libhello.so.1.0 HELLO.O establishes two additional symbolic connections again: $LN-S libhello.so.1.0 libhello.so.1$ln-s libhello.so.1 Libhello.so such a Libhello dynamic connection library is generated. The most important thing is to pass the gcc-shared parameter so that it is generated as a dynamic library rather than as a normal execution program. -WL indicates that the following parameter,-soname,libhello.so.1, is passed directly to the connector LD for processing. In fact, each library has a soname, and when the connector finds a name in the library it is looking for, the connector will embed soname inside the binary in the link, not the actual file name it is running, and during program execution, the program will look for files with soname names. Instead of the library's file name, in other words, Soname is the library's distinguishing flag. The purpose of this is to allow multiple versions of the system in the library file coexistence, is customary in the name of the library file is usually the same as Soname Libxxxx.so.major.minor where, xxxx is the name of the library, major is the main version number, minor is the minor version
--------------------------------------------------------------------------------summarize the analysis of the Linux library work, We can already understand how the program runs to find the "library" elsewhere, in the next article I continue to study the execution of executable programs, these two days to write a script on a server, almost successful. The time spent on Linux is noticeably less, and after two days of development, the small program will be back to the right line.
Iii. examples
Before creating a function library, let's prepare an example source program and compile the source program of the library into an. o file.
The 1th step: The program--hello.h, HELLO.C and main.c are given examples;
Hello.h (see program 1) is the header file for the function library.
HELLO.C (see program 2) is the source of the function library, which contains the public function hello, which will output "Hello xxx!" on the screen.
MAIN.C (see program 3) is the main program for the test library file, and the public function Hello is called in the main program.
--------------------------------------------------------------------------------
Program 1:hello.h
#ifndef Hello_h
#define Hello_h
void Hello (const char *name);
#endif//hello_h
--------------------------------------------------------------------------------
Program 2:HELLO.C
#include <stdio.h>
void Hello (const char *name)
{
printf ("Hello%s!\n", name);
}
--------------------------------------------------------------------------------
Program 3:MAIN.C
#include "hello.h"
int main ()
{
Hello ("everyone");
return 0;
}
--------------------------------------------------------------------------------
2nd step: Compile the hello.c into an. o file;
Whether a static library or a dynamic library, it is created by an. o file. Therefore, we must first compile the source program hello.c through GCC into an. o file.
At the system prompt, type the following command to get the hello.o file.
# gcc-c HELLO.C
#
(Note 1: This article does not describe the use of commands and their parameters, if you want to learn more about them, refer to other documents.) )
(Note 2: The first character "#" is the system prompt, you do not need to type, the following is the same.) )
We run the LS command to see if the hello.o file survived.
# ls
HELLO.C hello.h hello.o main.c
#
(Note 3: The first character is not "#" to run the results for the system, same as below.) )
In the results of the LS command, we see the hello.o file, which is done in this step.
Let's start by looking at how to create a static library and use it.
--------------------------------------------------------------------------------
3rd step: Create a static library from the. o file;
The naming convention for Static library filenames is prefixed with lib, followed by a static library name with a. a extension. For example: We will create a static library named Myhello, then the static library file name is LIBMYHELLO.A. You need to be aware of this when creating and using static libraries. Create a static library with the AR command.
At the system prompt, type the following command to create a static library file, Libmyhello.a.
# ar CR libmyhello.a hello.o
#
We also run the LS command to see the results:
# ls
hello.c hello.h hello.o libmyhello.a main.c
#
There are libmyhello.a in the results of the LS command.
--------------------------------------------------------------------------------
4th step: Use the static library in the program;
The static library is finished, how to use its internal function? You only need to include a prototype declaration of these common functions in the source program that uses these common functions, and then specify the static library name when you generate the target file with the GCC command, and GCC will connect the public function to the destination file from the static library. Notice that GCC adds the prefix lib to the static library name and then appends the extension. A to the static library file name to find the static library files.
In program 3:MAIN.C, we include the header file hello.h of the static library, and then call the public function hello directly in main program main. The following Mr. into the target program Hello, then run the Hello program to see how the results.
# gcc-o Hello main.c-l.-lmyhello
#./hello
Hello everyone!
#
Let's delete the static library file and try the common function hello whether it is really connected to the target file hello.
# RM LIBMYHELLO.A
Rm:remove regular file ' libmyhello.a '? Y
#./hello
Hello everyone!
#
The program runs as usual, and the public functions in the static library are already connected to the target file.
Let's continue to look at how to create a dynamic library in Linux. Let's start with the. o file.
--------------------------------------------------------------------------------
5th step: Create a dynamic library file from an. o file;
The dynamic library file name specification is similar to the static library file name naming specification and also adds the prefix lib to the dynamic library name, but its file name extension is. So. For example: We will create a dynamic library named Myhello, then the dynamic library file name is libmyhello.so. Use GCC to create a dynamic library.
At the system prompt, type the following command to get the dynamic library file libmyhello.so.
# Gcc-shared-fpci-o libmyhello.so hello.o
#
We still use the LS command to see if the dynamic library file is generated.
# ls
hello.c hello.h hello.o libmyhello.so main.c
#
--------------------------------------------------------------------------------
The 6th step: Use the dynamic library in the program;
Using a dynamic library in a program is exactly the same as using a static library, and it is also a prototype declaration that includes these common functions in the source program that uses these common functions, and then indicates that the dynamic library name is compiled when the target file is generated with the GCC command. We run the GCC command first to generate the target file, and then run it to see the results.
# gcc-o Hello main.c-l.-lmyhello
#./hello
./hello:error while loading shared libraries:libmyhello.so:cannot open Shared object file:no such file or directory
#
Oh! Something went wrong. Quick look at the error prompt, the original is not found dynamic library file libmyhello.so. At run time, the program looks for the required dynamic library files in directories such as/usr/lib and/lib. If found, loads the dynamic library, otherwise it will prompt similar errors to terminate the program. We'll copy the file libmyhello.so to the directory/usr/lib and try again.
# MV Libmyhello.so/usr/lib
#./hello
Hello everyone!
#
It worked. This further illustrates that a dynamic library is required when the program is running.
We looked back and found that using a static library and using a dynamic library to compile the GCC commands used by the target program is exactly the same as when the static library and the dynamic library have the same name, which library file does the GCC command use? Hold on to the problem will be to the end of the mood, to try.
Remove all files except. C and. h and revert to the state of the example program we just finished editing.
# rm-f Hello hello.o/usr/lib/libmyhello.so
# ls
HELLO.C hello.h MAIN.C
#
In order to create a static library file libmyhello.a and a dynamic library file libmyhello.so.
# gcc-c HELLO.C
# ar CR libmyhello.a hello.o
# Gcc-shared-fpci-o libmyhello.so hello.o
# ls
hello.c hello.h hello.o libmyhello.a libmyhello.so main.c
#
With the last ls command above, you can find that both the static library file libmyhello.a and the dynamic library file libmyhello.so have been generated and are in the current directory. We then run the GCC command to use the function library Myhello to generate the target file Hello and run the program hello.
# gcc-o Hello main.c-l.-lmyhello
#./hello
./hello:error while loading shared libraries:libmyhello.so:cannot open Shared object file:no such file or directory
#
It is easy to know from the results of the program Hello run that when the static library and the dynamic library have the same name, the GCC command takes precedence over the dynamic library.
Reference:
1. GCC http://baike.baidu.com/view/4848.htm
"Reprint" The parameters and commands commonly used in GCC