Steps for Linux to build a static link library

  

Usually we call the static link library is a .lib file, the file is generally large, can compile functions and data into a .lib file, then how to establish a static link in the Linux system What about the library? The following small series will introduce you to the method of establishing a static link library in Linux.

There are two main steps:

1. Compile the source code

2. Make the library

============= ===================================== Use two simple source code as an example to demonstrate the specific process of static library production

1. Compile source

hello_first.c

void hello_first(void)

<

printf(“hello first”);

}

hello_second.c

void hello_second(void)

{

printf(“hello second”);

}

Compile:

gcc -c hello_first.c -o hello_first.o

gcc -c hello_second.c -o hello_second.o

2. Make a static library

ar -r libhello.a hello_first.o hello_second.o

Call the library instance

hello_main.c

#include "stdio.h"

void hello_first(void);

void hello_second(void);

int main()

{

Hello_first();

hello_second();

return 0;

}

Compile:

First method:< Br>

gcc hello_main.c libhello.a -o hello_main

Second method:

Copy libhello.a to /lib directory

gcc hello_main. c -lhello -o hello_main2

run shot:

OK! The static library call succeeded! !

Thinking:

What is the role of static libraries? Why invent it?

1. Static library is mainly used to facilitate programmer programming, the function to be used is packaged into a library, just tell them the function interface, so that the programmer only knows how to call it. The manufacturer can keep the interface unchanged and modify and maintain the code.

2. It is also possible that the programmer himself encapsulates the functions he often uses, without having to rewrite it, just call the declaration before you can.

The above is the introduction of Linux to establish a static link library. When making a static link library, you need to compile the source code first, and then make a static link library.

Copyright © Windows knowledge All Rights Reserved