In Linux, how can I use GCC to compile programs and generate static and dynamic libraries.

Source: Internet
Author: User
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.

  1. /*
  2. * Hello. c
  3. */
  4. # Include <stdio. h>
  5. Int main ()
  6. {
  7. Printf ("Hello, world! /N ");
  8. Return 0;
  9. }

1.
Generate executable programs directly

  1. $ Gcc-O hello. c
  2. $./Hello
  3. Hello, world!
  4. For example
    Compilation Method
    The results are the same:
  5. $ GCC hello. C-O hello
  6. $./Hello
  7. Hello, world!
  8. 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 ):
  9. $ GCC hello. c
  10. $./A. Out
  11. Hello, world!

2.
Generate the pre-processed file hello. I

  1. $ Gcc-e hello. C-O hello. I
  2. $ Ls
  3. A. Out hello. c hello. I
  4. Hello. I.
    Is a new file
  5. The result of the following statement is the same:
  6. $ Gcc-e-o hello. I hello. c
  7. For example
    If no output file is set, it is printed to the standard terminal. You can view the output file in less mode:
  8. $ Gcc-e hello. c | less
  9. #1 "Hello. c"
  10. #1 "<built-in>"
  11. #1 "<command line>"
  12. #1 "Hello. c"
  13. #1 "/usr/include/stdio. H" 1 3 4
  14. #28 "/usr/include/stdio. H" 3 4
  15. #1 "/usr/include/features. H" 1 3 4
  16. #329 "/usr/include/features. H" 3 4
  17. ..............................
  18. Or
    Executed:
  19. $ Gcc-e hello. C-O hello. I
  20. $ VI hello. I
  21. 1 #1 "Hello. c"
  22. 2 #1 "<built-in>"
  23. 3 #1 "<command line>"
  24. 4 #1 "Hello. c"
  25. 5 #1 "/usr/include/stdio. H" 1 3 4
  26. 6 #28 "/usr/include/stdio. H" 3 4
  27. 7 #1 "/usr/include/features. H" 1 3 4
  28. 8 #329 "/usr/include/features. H" 3 4
  29. ...... <
    The middle part is omitted> ..................
  30. 929 #844 "/usr/include/stdio. H" 3 4
  31. 930
  32. 931 #2 "Hello. c" 2
  33. 932
  34. 933 int main ()
  35. 934 {
  36. 935 printf ("Hello, world! /N ");
  37. 936
  38. 937 return 0;
  39. 938}
  40. 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

  1. $ Gcc-s hello. C-O hello. s
  2. $ Ls
  3. A. Out hello. c hello. I hello. s
  4. Hello. s
    Is the newly generated file.
  5. The result of the following statement is the same:
  6. $ Gcc-S-O hello. s hello. c
  7. For example
    The following statement results are the same:
  8. $ Gcc-s hello. c
  9. Also
    You can generate an assembly file using an intermediate file generated in the previous step:
  10. $ Gcc-s hello. I-O hello. s
  11. $ Gcc-S-O hello. s hello. I
  12. $ Gcc-s hello. I
  13. Raw
    The Assembly Code is as follows:
  14. $ VI hello. s
  15. 1. File "Hello. c"
  16. 2. Section. rodata
  17. 3. lc0:
  18. 4. String "Hello, world! "
  19. 5. Text
  20. 6. globl main
  21. 7. type main, @ Function
  22. 8 main:
  23. 9 Leal 4 (% ESP), % ECx
  24. 10 andl $-16, % ESP
  25. 11 pushl-4 (% ECx)
  26. 12 pushl % EBP
  27. //
    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

  1. $ Gcc-C hello. C-O hello. o
  2. $ Ls
  3. A. Out hello. c hello. I hello. O hello. s
  4. Hello. o
    Is the newly generated target file:
  5. The result of the following statement is the same:
  6. $ Gcc-c-o hello. O hello. c
  7. For example
    The following statement results are the same:
  8. $ Gcc-C hello. c
  9. Also
    You can use the intermediate file hello. I or hello. s generated in the previous step to generate the target file:
  10. $ Gcc-C hello. I
  11. $ Gcc-C hello. s
  12. Me
    You can use objdump to view the binary code of Hello. O:
  13. $ Objdump-s hello. o
  14. Hello. O: File Format elf32-i386
  15. Contents of section. Text:
  16. 0000 8d4c2404 83e4f0ff 71fc5589 e55183ec. L $ ...... Q. U ..
  17. 0010 04c70424 00000000 e8fcffff ffb80000 ...... $ ............
  18. 0020 listen 83c4 04595d8d 61fcc3 ...... y]. ..
  19. Contents of section. rodata:
  20. 0000 68656c6c 6f2c2077 6f450c64 2100 Hello, world !.
  21. Contents of section. Comment:
  22. 0000 00474343 3a202847 4e552920 342e312e. GCC: (GNU) 4.1.
  23. 0010 31203230 30373031 30352028 52656420 1 20070105 (red
  24. 0020 48617420 342e312e 312d3532 2900 hat 4.1.1-52 ).

5.
Generate executable programs using intermediate files

  1. $ Gcc-O hello. I
  2. $./Hello
  3. Hello, world!
  4. $ Gcc-O hello. s
  5. $./Hello
  6. Hello, world!
  7. $ Gcc-O hello. o
  8. $./Hello
  9. 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

  1. /*
  2. * Main. c
  3. */
  4. # Include <stdio. h>
  5. Extern int func1 ();
  6. Extern int func2 ();
  7. Int main ()
  8. {
  9. Int I;
  10. I = func1 ();
  11. Printf ("func1 return = % d/N", I );
  12. I = func2 ();
  13. Printf ("func2 return = % d/N", I );
  14. Return 0;
  15. }

-----------------------------------------------------

  1. /*
  2. * Func1.c
  3. */
  4. Int func1 ()
  5. {
  6. Return 100;
  7. }

-----------------------------------------------------

  1. /*
  2. * Func2.c
  3. */
  4. Int func2 ()
  5. {
  6. Return 200;
  7. }

I
Compilation refers
Order:

  1. $ Gcc-C func1.c
  2. $ Gcc-C func2.c
  3. $ Ls
  4. Func1.c func1.o func2.c func2.o main. c
  5. Func1.o and func2.o are
    The target file we generated. The packaging command is as follows:
  6. $ Ar-r libfunc. A func1.o func2.o
  7. Me
    View the files in libfunc.:
  8. $ Ar-T libfunc.
  9. Func1.o
  10. Func2.o
  11. Now
    Use the static library and Main. C to generate the target program:
  12. $ Gcc-O main. c libfunc.
  13. $./Main
  14. Func1 return = 100
  15. Func2 returned = 200
  16. 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:

  1. First
    First, we generate the target file, but we need to add the compiler option-FPIC and linker option-shared
  2. $ Gcc-FPIC-C func1.c
  3. $ Gcc-FPIC-C func2.c
  4. $ Gcc-shared-O libfunc. So func1.o func2.o
  5. $ Ls
  6. Func1.c func1.o func2.c func2.o libfunc. So main. c
  7. Libfunc. So
    Is the target dynamic library we generated. We use the dynamic library and Main. C to generate the target program:
  8. $ Gcc-O main. C-L.-lfunc
  9. 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:
  10. $./Main
  11. ./Main: Error while loading shared libraries: libfunc. So: cannot open shared object file: no such file or directory
  12. 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:
  13. $ Export LD_LIBRARY_PATH = 'pwd'
  14. $./Main
  15. Func1 return = 100
  16. Func2 returned = 200
  17. Operation
    The row is successful. Now we can change the function of the dynamic library without re-linking. As follows:
  18. Change func1.c:
  19. Int func1 ()
  20. {
  21. Return 101;
  22. }
  23. More
    Change func2.c:
  24. Int func2 ()
  25. {
  26. Return 202;
  27. }
  28. Heavy
    New Database generation:
  29. $ Gcc-FPIC-shared func1.c func2.c-O libfunc. So
  30. $./Main
  31. Func1 return = 101
  32. Func2 returned = 202
  33. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.