GCC implements Multi-file compilation, static library, and dynamic library
Source: Internet
Author: User
Code 1
// Add. Hint add (int A, int B );
// Add. CINT add (int A, int B) {return a + B ;}
// Main. c
# Include <stdio. h> # include "Add. H"
Void main () {printf ("3 + 2 = % d \ n", add (3 + 2 ));}
Multi-file compilation 1 generate. o file command: gcc-C add. C main. c generate: Add. O, Main. o
2 generate the executable file command: gcc-O main Add. O main. O generate: Main
3 run the command:./main output: 3 + 2 = 5
Static Library 1 generate the. o file command: gcc-C add. c generate: Add. o
2 generate libxxx. A command: Ar RCS libadd. A Add. O generate: libadd.
3. Generate the executable file command: gcc-O main. C-static-L.-Ladd: Main
4 run the command:./main output: 3 + 2 = 5
Three dynamic library 1 generate the. o file command: gcc-C add. c generate: Add. o
2 generate libxxx. So command: gcc-shared-FPIC-O libadd. So add. O generate: libadd. So
3. Dynamic library Path Method 1: copy the file to the directory/usr/lib and run the following command: MV libadd. So/usr/lib.
Method 2: Set LD_LIBRARY_PATH to the current directory. Command: Export LD_LIBRARY_PATH =$ (PWD)
4. Generate the executable file command: gcc-O main. C-L.-Ladd: Main
# If Step 4 is not displayed, the following error occurs: libadd. So cannot be found.
5 run the command:./main output: 3 + 2 = 5
4. makefile # MF: mult File
# LIBA: liaxxx.
# Libso: libxxx. So
Run: Main
./Main
MFO: Add. C main. c
Gcc-C add. C main. c
MFE: MFO
Gcc-O main Add. O main. o
LiBo: Add. c
Gcc-C add. c
LIBA: Libo
Ar RCS libadd. A Add. o
Libae: LIBA
Gcc-O main. C-static-L.-Ladd
Libso: Libo
Gcc-shared-FPIC-O libadd. So add. o
Libsoe: libso
Export LD_LIBRARY_PATH = $ (PWD)
Gcc-O main. C-L.-Ladd
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.