Linux static library and dynamic library instance detail

  • 2020-06-19 12:29:36
  • OfStack

Linux static and dynamic library instances in detail

1. Compilation and use of static link libraries under Linux

First write the following code:


// main.c
#include "test.h"
int main(){
  test();
  return 0;  
}

// test.h
#include<iostream>
using namespace std;
void test();

// test.c
#include "test.h"
void test(){
 cout<< "test!" <<endl;
}

Then compile:

gcc-c test. c // Generate target files
ar crv libtest.a test.o // Generate static link library libtest.a
3. g++ -ES28en main main. c-ltest // Compile main program and link libtest. a static library
4../main // Run main program

2. Compilation and use of dynamic link libraries under Linux

The code corresponds to 1 above.

Then compile:

1. g++ -ES52en-ES53en libtest.so test // Generate dynamic link library libtest. so
g++ -ES62en main main // call dynamic link library libtest.so
3../main // Run main program

3. Relevant target file is missing when linking (.o)

The code corresponds to 1 above.

The compilation process is as follows:

1. gcc -c test.c
2. gcc -c main.c
3. gcc -o main main.o

At this point, you will find that the error is: undefined reference to 'test'.

This is the most typical undefined reference error because it is found that the implementation file for a function cannot be found while linking. In this case, the test.o file contains the implementation for the test() function, so it is ok to link as follows.


1. gcc -o main main.o test.o



 [Extension] : In fact, in order to give you a better understanding of the underlying reasons, I have separated the compilation link, the following compilation 
 Will be submitted to the undefined reference No, actually the underlying reason is the same as the above 1 Kind of. 

gcc -o main main.c // The lack of test() Implementation file of  

 It needs to be changed to the following form to succeed test() The implementation file of the function 1 The compilation. 

gcc -o main main.c test.c //ok, No problem 

4. Missing related library files when linking (.a /.so)


 Here, just a static library example, assuming the source with the above 1 Cause. 

1.  the test.c Compile to static library: 
  gcc -c test.c
  sr -rc test.a test.o
  gcc -c main.c

2.  Generate executable programs: 
  gcc -o main -main.o

     And that also happens at this point  undefined reference to `test' An error. The root cause is also not to be found test() The implementation of the function 
   Piece, as the test() The implementation of the function in test.a In this static library, so the link needs to be added after test.a this 
   Library, the link command can be modified to the following form. 
  1. gcc -o main main.c ./test.a

5. Multiple library file link sequence problem


 This kind of problem is also very covert, do not study carefully you may feel very puzzling. So let's go back to number one 3 Discussed in subsection 
 In the end, if we switch the order of the linked libraries 1 Now, what happens? 

1. gcc -o main main.o func.a test.a

 We get the following compilation error: 

1. test.a(test.o): In function `test': 
2. test.c:(.text+0x13): undefined reference to `func' 
3. collect2: ld returned 1 exit status 
 Therefore, we need to pay attention to the order of dependencies between libraries, depending on other libraries, when given the dependent libraries in the link command 
 In the library 1 Be sure to put it in front of the dependent library so you can really avoid it undefined reference Error to complete the compile link. 

If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, hope to help you, thank you for your support to this site!


Related articles: