Create and link static library instances with GCC in Linux

Source: Internet
Author: User
Differences between a static library and a dynamic library: static library (xxx. a): When GCC compiles an executable program, it extracts necessary target files from the static library. When the executable program is compiled successfully, it also contains the target file, the disadvantage is that when multiple executable programs call a library file at the same time, the library files loaded into the memory may be duplicated.

Differences between a static library and a dynamic library:

Static Library (xxx. a): When GCC compiles an executable program, it extracts necessary target files from the static library. When the executable program is compiled successfully, it also contains the target file, the disadvantage is that when multiple executable programs call a library file at the same time, the library files loaded into the memory may be duplicated, which is a huge waste of memory, however, because the target program required in the library file has been copied during compilation, the initial loading speed of the executable program is slightly faster than that of the dynamic library.

Dynamic library (xxxx. so): When GCC compiles and generates an execution program, it is only the address of a library (which can be understood as a pointer) copied to the executable program ), in this way, when multiple executable programs use the same dynamic library, only one copy of the library files loaded into the memory is required. This greatly protects the precious memory resources, it takes a little time to find the dynamic library file through the address when executing the executable file.

 

 

Below is an instance used for creating a static Library:

 

[Root @ localhost ~] #Cd/Gcc/ar/

[Root @ localhost ar] #MkdirINcLude

[Root @ localhost ar] # mkdir lib

Create a header file: mylib. h

[Root @ localhost ar] # vim include/mylib. h

 

Int func1 (int x, int y );

 

VoIdFunc2 (int x );

Create the following source files: main. c, func1.c, func2.c

[Root @ localhost ar] # vim main. c

 

# Include

# Include "mylib. h"

 

Int main (void)

{

Int I;

I = func1 (1, 2 );

Func2 (I );

Return 0;

}

~

[Root @ localhost ar] # vim func1.c

 

# Include "mylib. h"

 

Int func1 (int x, int y)

{

Return (x + y );

}

~

[Root @ localhost ar] # vim func2.c

 

# Include

# Include "mylib. h"

 

Void func2 (int x)

{

Printf ("The reSuLt is % d \ n ", x );

}

~

Generate the target file

[Root @ localhost ar] # gcc-Wall-O-c func1.c? Linclude

[Root @ localhost ar] # gcc-Wall-O-c func2.c? Iinclude

[Root @ localhost ar] # gcc-Wall-O-c main. c? Iinclude

Generate library files

[Root @ localhost ar] # cd lib/

[Root @ localhost lib] # ar cr libethnicity. a ../func1.o ../func2.o

[Root @ localhost lib] # ar t libethnicity.

Func1.o

Func2.o

Compile and generate executable files (three methods are used here)

1) Adopt the-L library and-I include Method

[Root @ localhost ar] # gcc-Wall-O main. o-Llib-lethnicity-o wanyan

[Root @ localhost ar] #./wanyan

The result is 3

2) Add to system library

[Root @ localhost ar] #CpLib/libethnicity. a/usr/lib

[Root @ localhost ar] # gcc-Wall-O main. o-lethnicity-o ethnicitybeta

[Root @ localhost ar] #./ethnicitybeta

The result is 3

3) modify local. bash_proFileMethod

[Root @ localhost ar] #ExPort LIBRARY_PATH =/gcc/ar/lib: $ LIBRARY_PATH

[Root @ localhost ar] # gcc-Wall-O main. o-lethnicity-o laji

[Root @ localhost ar] #./laji

The result is 3

Lab ended

 

Summary: this small experiment is a note for instructor Xiao Bu's video study. The code used is very simple because this experiment aims to understand how to build a static library and stay away from it, finally, three methods for loading static libraries during GCC compilation are listed, which are very practical.

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.