How to Use GCC to compile programs and generate simple programs in Linux
Static library and dynamic library.
This article applies to Linux developers. This article explains how to use GCC to compile programs, generate static libraries and dynamic libraries in Linux.
1. About installation. Generally, the compiler is installed by default, and there is a lot of information on the network to introduce the installation problems in different release versions. This article will not describe it.
II. Introduction to file extensions in C Programming
. A static library (package files)
. C source code without preprocessing
. H c header file
. I pre-processed C source code
Target file generated after. O Compilation
Assembly language code generated by. s
. So
Dynamic library (Dynamic Link Library)
Explanation: *. A is the static library that we use AR to package and generate after compilation. *. c generally makes the code we edit, and the result of our work. *. H is generally
The manually generated interface file can also be found in *. after C is complete, use the GCC option-aux-info to help us generate it ;*. I is the source code after preprocessing, which is automatically generated by GCC under option-e compilation.
*. O is a compilation.
The target file generated after translation ;*. S is the assembly language code generated by GCC under option-s compilation. For programs with high performance requirements, you can compile the assembly language file and optimize the assembly.
After compilation, the target file is generated and linked; *. So is a dynamic library, which is generated using the-FPIC-shared option of GCC.
Iii. compilation process of Hello. c
The demos in this section are for the file hello. C.
- /*
- * Hello. c
- */
- # Include <stdio. h>
- Int main ()
- {
- Printf ("Hello, world! /N ");
- Return 0;
- }
1.
Generate executable programs directly
- $ Gcc-O hello. c
- $./Hello
- Hello, world!
- For example
Compilation Method
The results are the same:
- $ GCC hello. C-O hello
- $./Hello
- Hello, world!
- For example
Compilation Method
Different from the above Compilers
Case (for details about the network data with different elf and A. out file formats, there is no difference in the results here ):
- $ GCC hello. c
- $./A. Out
- Hello, world!
2.
Generate the pre-processed file hello. I
- $ Gcc-e hello. C-O hello. I
- $ Ls
- A. Out hello. c hello. I
- Hello. I.
Is a new file
- The result of the following statement is the same:
- $ Gcc-e-o hello. I hello. c
- For example
If no output file is set, it is printed to the standard terminal. You can view the output file in less mode:
- $ Gcc-e hello. c | less
- #1 "Hello. c"
- #1 "<built-in>"
- #1 "<command line>"
- #1 "Hello. c"
- #1 "/usr/include/stdio. H" 1 3 4
- #28 "/usr/include/stdio. H" 3 4
- #1 "/usr/include/features. H" 1 3 4
- #329 "/usr/include/features. H" 3 4
- ..............................
- Or
Executed:
- $ Gcc-e hello. C-O hello. I
- $ VI hello. I
- 1 #1 "Hello. c"
- 2 #1 "<built-in>"
- 3 #1 "<command line>"
- 4 #1 "Hello. c"
- 5 #1 "/usr/include/stdio. H" 1 3 4
- 6 #28 "/usr/include/stdio. H" 3 4
- 7 #1 "/usr/include/features. H" 1 3 4
- 8 #329 "/usr/include/features. H" 3 4
- ...... <
The middle part is omitted> ..................
- 929 #844 "/usr/include/stdio. H" 3 4
- 930
- 931 #2 "Hello. c" 2
- 932
- 933 int main ()
- 934 {
- 935 printf ("Hello, world! /N ");
- 936
- 937 return 0;
- 938}
- Yes
As you can see, there are nearly 1000 lines of code, and we only account for the last 8 lines.
3. Generate assembly language files
Hello. s
- $ Gcc-s hello. C-O hello. s
- $ Ls
- A. Out hello. c hello. I hello. s
- Hello. s
Is the newly generated file.
- The result of the following statement is the same:
- $ Gcc-S-O hello. s hello. c
- For example
The following statement results are the same:
- $ Gcc-s hello. c
- Also
You can generate an assembly file using an intermediate file generated in the previous step:
- $ Gcc-s hello. I-O hello. s
- $ Gcc-S-O hello. s hello. I
- $ Gcc-s hello. I
- Raw
The Assembly Code is as follows:
- $ VI hello. s
- 1. File "Hello. c"
- 2. Section. rodata
- 3. lc0:
- 4. String "Hello, world! "
- 5. Text
- 6. globl main
- 7. type main, @ Function
- 8 main:
- 9 Leal 4 (% ESP), % ECx
- 10 andl $-16, % ESP
- 11 pushl-4 (% ECx)
- 12 pushl % EBP
- //
Note: If you are familiar with it, you can optimize part of the assembly to achieve better results.
4. Generate the target file
Hello. o
- $ Gcc-C hello. C-O hello. o
- $ Ls
- A. Out hello. c hello. I hello. O hello. s
- Hello. o
Is the newly generated target file:
- The result of the following statement is the same:
- $ Gcc-c-o hello. O hello. c
- For example
The following statement results are the same:
- $ Gcc-C hello. c
- Also
You can use the intermediate file hello. I or hello. s generated in the previous step to generate the target file:
- $ Gcc-C hello. I
- $ Gcc-C hello. s
- Me
You can use objdump to view the binary code of Hello. O:
- $ Objdump-s hello. o
- Hello. O: File Format elf32-i386
- Contents of section. Text:
- 0000 8d4c2404 83e4f0ff 71fc5589 e55183ec. L $ ...... Q. U ..
- 0010 04c70424 00000000 e8fcffff ffb80000 ...... $ ............
- 0020 listen 83c4 04595d8d 61fcc3 ...... y]. ..
- Contents of section. rodata:
- 0000 68656c6c 6f2c2077 6f450c64 2100 Hello, world !.
- Contents of section. Comment:
- 0000 00474343 3a202847 4e552920 342e312e. GCC: (GNU) 4.1.
- 0010 31203230 30373031 30352028 52656420 1 20070105 (red
- 0020 48617420 342e312e 312d3532 2900 hat 4.1.1-52 ).
5.
Generate executable programs using intermediate files
- $ Gcc-O hello. I
- $./Hello
- Hello, world!
- $ Gcc-O hello. s
- $./Hello
- Hello, world!
- $ Gcc-O hello. o
- $./Hello
- Hello, world!
IV,
Static library generation
It is easier to generate static libraries in Linux. Use ar after generating the target file
Package. In medium and large projects, a module is generally made into a static library to facilitate management, improve compilation, and link efficiency.
This section is intended
Main. C, func1.c, and func2.c files
- /*
- * Main. c
- */
- # Include <stdio. h>
- Extern int func1 ();
- Extern int func2 ();
- Int main ()
- {
- Int I;
- I = func1 ();
- Printf ("func1 return = % d/N", I );
- I = func2 ();
- Printf ("func2 return = % d/N", I );
- Return 0;
- }
-----------------------------------------------------
- /*
- * Func1.c
- */
- Int func1 ()
- {
- Return 100;
- }
-----------------------------------------------------
- /*
- * Func2.c
- */
- Int func2 ()
- {
- Return 200;
- }
I
Compilation refers
Order:
- $ Gcc-C func1.c
- $ Gcc-C func2.c
- $ Ls
- Func1.c func1.o func2.c func2.o main. c
- Func1.o and func2.o are
The target file we generated. The packaging command is as follows:
- $ Ar-r libfunc. A func1.o func2.o
- Me
View the files in libfunc.:
- $ Ar-T libfunc.
- Func1.o
- Func2.o
- Now
Use the static library and Main. C to generate the target program:
- $ Gcc-O main. c libfunc.
- $./Main
- Func1 return = 100
- Func2 returned = 200
- And
Our expectations are consistent. Next we will go to the dynamic library.
5. Dynamic library generation
The dynamic library generation in Linux is implemented using the GCC option. The case programs are the same as those in the static library. Here is the operation command:
- First
First, we generate the target file, but we need to add the compiler option-FPIC and linker option-shared
- $ Gcc-FPIC-C func1.c
- $ Gcc-FPIC-C func2.c
- $ Gcc-shared-O libfunc. So func1.o func2.o
- $ Ls
- Func1.c func1.o func2.c func2.o libfunc. So main. c
- Libfunc. So
Is the target dynamic library we generated. We use the dynamic library and Main. C to generate the target program:
- $ Gcc-O main. C-L.-lfunc
- Note
-L.-lfunc is used as the compilation option. -L. The table searches for the required dynamic library from the current directory.-lfunc is the call rule for the dynamic library. Name of the dynamic library in Linux
The format is lib *. So, And the link represents the bit-L *, * is the name of your own database. Run the following command:
- $./Main
- ./Main: Error while loading shared libraries: libfunc. So: cannot open shared object file: no such file or directory
- Submit
Indicates an error,
Indicates that the dynamic library cannot be found. In Linux, the most convenient solution is to copy libfunc. So to the absolute directory/lib. However, only Super Users have this permission. Another solution
Is to change the environment variable LD_LIBRARY_PATH. As follows:
- $ Export LD_LIBRARY_PATH = 'pwd'
- $./Main
- Func1 return = 100
- Func2 returned = 200
- Operation
The row is successful. Now we can change the function of the dynamic library without re-linking. As follows:
- Change func1.c:
- Int func1 ()
- {
- Return 101;
- }
- More
Change func2.c:
- Int func2 ()
- {
- Return 202;
- }
- Heavy
New Database generation:
- $ Gcc-FPIC-shared func1.c func2.c-O libfunc. So
- $./Main
- Func1 return = 101
- Func2 returned = 202
- Yes
We can see that the dynamic library has been updated.
Vi. Conclusion
This article briefly introduces how to use GCC to compile programs in Linux and how to simplify
Single Static and Dynamic library generation. The static library provides a packaging management solution, while the dynamic library updates the program to a certain extent. More importantly, when multiple instances exist, dynamic library can reduce memory consumption
(Only one part of code space is occupied ).
Those interested in this series of knowledge can continue to follow up and read subsequent articles: Library version management, GCC compilation options, makefile and automated compilation.