Jeremy Lin
about GCC
The GNU complier Collection is the GNU's powerful, performance-rich multiplatform compiler suite that includes the front end of C, C + +, objective-c, Fortran, Java, Ada, and go languages. A library of these languages is included, and the latest version is GCC 5.1. GCC compiles executable programs on a variety of hardware platforms and performs 20% to 30% more efficiently than average compilers. The GCC compiler compiles and connects C, C + + language source, sink, and target programs into an executable file, and GCC generates a file named A.out if it is not given the name of the executable. In Linux systems, executables do not have a uniform suffix, and the system distinguishes between executables and non-executable files from the properties of a file. GCC uses a suffix to differentiate between the types of input files, and here we describe some of the Convention rules that GCC follows:
. c file C language source code file;
(a) A file of the archive consisting of the target document;
. c,.cc or. cxx file C + + source code files and must be preprocessed;
The header file contained in the. h file program;
. i file C source code file and should not perform preprocessing;
. ii file C + + source code files and should not be preprocessed;
. m file objective-c source code file;
. mm file objective-c++ source code file;
The. o file after the compiled target file;
. s file assembly language source code files;
. s files are pre-compiled assembly language source code files.
gcc Execution Process
Although we call GCC a C compiler, the process for GCC to generate executables from the C language source code is not just the process of compiling, but it is going through 4 interrelated steps:
(1) pretreatment (preprocessing)
(2) compile (compilation)
(3) compilation (Assembly)
(4) connection (linking)
(1) Preprocessing stage: the input is the C language source file, usually *.c. They usually come with header files such as. h. This phase mainly deals with #ifdef, #include和 # define commands in the source file. This phase generates an intermediate file *.i, but it is not typically generated in the actual work, because it is basically not available, but you can use the following command if you want to generate such a file:
GCC-E Hello.c-o hello.i
The source code for HELLO.C is as follows:
#include <stdio.h>int main (int argc, CHAR**ARGV) {printf ("Hello linux\n"); return 0;}
The results of the generated hello.i are as follows (very long, so take only a partial look)
......
......
(2) Compile stage: the input is intermediate file *.i, compiled assembly language file *.S. The GCC commands that correspond to this phase are as follows:
Gcc-s Hello.i-o Hello.s
is as follows:
(3) assembly stage: The input is the assembly file *.s, output conversion generated machine language *.O. This stage corresponds to the following command:
Gcc-c Hello.s-o hello.o
(4) connection stage: *.S the input machine code file into an executable binary file. The command is as follows:
GCC Hello.o-o Hellox
gcc basic usage and options
When using the GCC compiler, we must give a list of necessary invocation parameters and file names. The GCC compiler has more than 100 invocation parameters, most of which we may not be able to use at all, and only the most centralized and commonly used parameters are described here.
Basic uses of GCC:
gcc [options] [filenames]
Where options are the parameters required by the compiler, filenames gives the relevant file names.
-C, compiles only, does not connect as an executable file. The compiler only generates *.O target files from source code files such as input *.c, and is usually used to compile subroutine files that do not contain the main program;
-O output_filename, determine that the output file name is Output_filename, and that the name cannot be the same as the source file. If this option is not given, GCC gives the default executable file a.out;
-G, the symbolic information necessary to produce the symbolic debugging tool (GNU GDB), to debug the source code, we must add this option;
-O, the program to optimize the compilation, connection, the use of this option, the entire source code will be in the compilation, connection process optimization processing, the resulting executable file execution efficiency can be improved, but the compilation, connection speed corresponding to a bit slower;
-o2, better than-O to optimize the compilation, connection, the corresponding speed will be more slowly;
-wall, compile warning option, if GCC encounters some of the appropriate warning and prompt messages in the compilation process where it deems it possible that an error occurred;
-I dirname, adding the directory indicated by DirName to the program header file search directory list, is the parameter used in the pre-compilation process;
-S, compiles the specified source file, but does not compile;
-E, preprocessing the specified source file, without compiling;
compiling a static library
A static library is a collection of some target code. Static library target files in Linux environments typically use. A as the file name extension of the target file.
Use the AR command to create a static library in a Linux environment. The advantage of a static library is that it is simple to use and fast to compile. The static library is compiled into a relocatable target file when the application is built, so it is unnecessary to compile, save compilation time, and generate executable programs in the shortest time possible. Also, the static library is copied to the executable code at compile time, and does not need to be linked again when the program loads as if it were using a dynamic library. In theory, an executable program uses a static library, which is 1%~5% faster than using a dynamic library.
(1) Create a static library
Suppose you have a source code STATIC_LIB.C:
int add (int a, int b) {return a + B;} int sub (int a, int b) {return a-A;} int mul (int a, int b) {return a * b;} int div (int a, int b) {return a/b;}
now to compile it into a static library file--STATIC_LIB.A, you can use the following command:
Gcc-c Static_lib.car RCS STATIC_LIB.A STATIC_LIB.C
where AR can add the target file to an existing static library (if it does not exist), the RCS parameters are: Add the target file in the list to the static library (parameter R), if the specified static library does not exist, then create the static library file (parameter C), and finally update the index of the static library file. Make it contain the contents of the newly added target file (parameter s).
(2) using a static library
After the static library is created successfully, you need to link to the application for use. Applications can reference functions or global variables in the library. In order for the application to correctly reference the global symbols in the library, you need to make a file that contains the global symbol declarations in that static library. This header file can be included in the application's header file so that it can reference the global symbols in the static library.
Header file Static_lib.h
extern int Add (int a, int b), extern int sub (int a, int b), extern int mul (int a, int b), extern int div (int a, int b) ;
Application main.c:
#include <stdio.h> #include "static_lib.h" int main (void) {int A, b;printf ("Please input A and b \ n"); scanf ("%d%d", &A Mp;a, &b);p rintf ("The Add: %d", add (b));p rintf ("The Sub: %d", Sub (b));p rintf ("The Mul: %d", Mul (a , b));p rintf ("The div: %d", Div (A, b)); return 0;}
we use the-l option of GCC to specify a static library, or use the-l parameter to specify the search path for the library file. The-L and-L are followed directly with parameters without spaces. Thus, for this example, the Compile command is as follows:
GCC Main.c-lstatic_lib.a-o App
or use "." As the search path for the static library, which means searching for the required static library files in the current directory.
Gcc-l. Main.c-o App
Note:-L is a linker option and must be placed after the file name of the compiled source file.
GCC also supports linking to a static library using the-static option.
GCC main.c-static./static_lib.a-o app
compiling a dynamic library
Dynamic libraries are also known as shared libraries or dynamic-link libraries, and dynamic-link libraries are used extensively in modern programs. For example, DLL files in a Windows environment and so files in a Linux environment. A dynamic library is loaded when the program is started. When a program loads a dynamic library, other applications can still load the same dynamic library.
(1) Create a dynamic library
Suppose you have a code that needs to be converted into a dynamic library SHARE_LIB.C
void Insert_sort (int *array, int length) {int I, j;for (i = 1; i < length; ++i) {int temp;j = I;temp = Array[j];while (j > 0) {if (temp < array[j-1]) {array[j] = array[j-1];j--;} Elsebreak;} ARRAY[J] = temp;}} int binary_search (int *array, int item, int length) {int high, low, Mid;high = Length-1;low = 0;mid = (high + low)/2;w Hile (Low <= high) {if (Array[mid] > Item) {high = Mid;mid = (high + low)/2;} else if (Array[mid] < item) {low = Mid;mid = (high + low)/2;} Elsereturn Mid;} return-1;}
Linux uses GCC to create a dynamic library. Because a dynamic library can be loaded by multiple processes, a location-independent destination file needs to be generated. You need to use the-fpic option of the GCC compiler, which is used to generate location-independent code. In addition to using the-FPIC option, you need to use thethe-shared option, which makes location-independent code a dynamic library.
Gcc-shared-fpic share_lib.so SHARE_LIB.C
(2) Using dynamic libraries
When a dynamic library is created, an application can reference a function or global variable in the library. In order for the application to correctly reference the global symbols in the library, you need to make a header file that contains the global symbol declarations in the dynamic library.
such as Share_lib.h
extern void Insert_sort (int *array, int length), extern int binary_search (int *array, int item, int length);
Using the main.c of a dynamic library
#include <stdio.h> #include "share_lib.h" int main (int argc, char const *argv[]) {int array[5] = {5,4,2,3,1};int item; int Pos;insert_sort (array, 5);p rintf ("Please input a number\n"), scanf ("%d", &item);p OS = binary_search (array, item, 5); if (pos = =-1) { printf ("Can ' t find\n");} else{ printf ("The position is%d\n", pos+1);} return 0;}
Use the following command to create the program:
GCC main.c./share_lib.so-o app
This address: http://blog.csdn.net/linj_m/article/details/45039347
More resources please follow blog: linjm-machine vision Weibo: Lin Jianmin-Machine Vision
Linux growth path (gcc compilers, static libraries, dynamic libraries)