C language dynamic link library and static link library

Foreword: if you are used to using IDE (integrated development environment), or the IDE you used at the beginning, your brain is blank. You can only go online to find information, especially the knowledge of Library links. IDE is just a simple description: it includes the directory of the corresponding library and the import package, but what does the IDE do behind this sentence? The earlier you understand this underlying thing, the more time you can save

devc + + is a classic c/c++IDE, which adopts the GCC compiler of MINGW. Note that this compiler is a compiler transplanted from linux to windows, which is very important to clarify the whole process

The suffix of static library in linux is a. Dynamic library suffix is so, and the suffix of static library in windows is lib, dynamic library suffix is dll

Here we first introduce how to compile and generate with gcc in windows environment a and so file

We first find gcc in the dev CPP \ mingw64 \ bin folder, copy its path, and then create an empty folder a on disk F (any disk is OK)_ So (any name is OK), and then create one in this folder c file, full name a.c

We write the contents of the document like this,

int fun(int in){
    return in; 
}

Then open cmd, write the corresponding drive letter first, and then jump to the directory where gcc is located. The command line is as follows:

D:
cd D:.../Omit some intermediate directories/bin/ 

Type the following command

gcc -c F:\a_so\a.c -o F:\a_so\a.o

You got one o file, and then enter another command

ar -v -q F:\a_so\libstatic_lib.a F:\a_so\a.o

-v is to show the details of packaging: give you a list of packages, and -q is to force the overwriting of previously typed packages

So you put one o the file is packaged as a static library file libstatic_lib.a (the static library file should comply with the previous rule of LIB), and then we try to call to create test.c under the same level directory of a.c c. It says so

#include<stdio.h>
#include"a.h"
int main(){
   int a=fun(6);
   printf("%d",a); 
  getchar();
return 0; }

There is an a.h. in fact, our program cannot find the fun function, so we write a header file to let it find it. The contents of the header file are:

int fun(int in);

At this time, if we execute this instruction on the command line

gcc F:\a_so\test.c -o F:\a_so\test.exe -L"F:\a_so" -lstatic_lib -static-libgcc

-l is the specified library. According to the rules, all static libraries are libname The format of a can be abbreviated as - lname. Of course, you can also directly replace - L"" -lname with F: \ a_ so\libstatic_ lib. a. - l is the specified library directory, - static libgcc is the forced import of static libraries,

 

Then in a_so directory, you will see test Exe file, you will get this result after execution

 

Next is the dynamic library

Dynamic library creation:

In a_ Create so under so file C file, which reads:

int add(int a,int b){
    return a+b;    
}  
int multiplication(int a,int b){
    return a*b;
} 

Then on the command line:

gcc F:\a_so\so.c -o F:\a_so\libDynamic_lib.so --shared

so you get one so file, and then we call it dynamically

Create d_d_use.c. The document reads as follows:

#include<stdio.h>
#include<windows.h>
int main() {
    typedef int (*pfunc)(int x,int y);//Define function pointer 
    HINSTANCE dllHandle =LoadLibrary(TEXT("libDynamic_lib.so"));//Handle instance,Loading and warehousing,If it is dll Just change so by dll 
    pfunc pf1 = (pfunc)GetProcAddress(dllHandle,TEXT("add"));//Declare function pointer and assign value 
    pfunc pf2 = (pfunc)GetProcAddress(dllHandle,TEXT("multiplication"));
    if(pf1!=NULL) printf("%d\n",pf1(5,6));//call 
    if(pf2!=NULL) printf("%d\n",pf2(5,6));
    FreeLibrary(dllHandle);//release 
    return 0; 
} 

 

Then compile and run it directly in dev, and we will get the following results:

 

Just as expected (the low version windows library may not support the loading of so files), in fact, replace the output of dynamic link library with dll is the same result. I won't repeat the demonstration here

 

The next step is the static link dynamic library. The reason for saying this later is that in order to simplify, I did not export the function when creating the dynamic library

Next, create d_s_use.c. The document reads as follows:

#include<stdio.h>
__declspec(dllimport) int add(int a,int b);
__declspec(dllimport) int multiplication(int a,int b);
int main() {
    printf("%d\n",add(5,6));
    printf("%d\n",multiplication(5,6));
  getchar();
return 0; }
__ declspec(dllimport) means import function. If it is removed, it will become that we declare the function in the c file

After that, don't panic. Static import still needs to be done lib/.a file, we use the gcc command line to create it So. Of so file C use it to create as a documents

gcc F:\a_so\so.c -o F:\a_so\libDynamic_lib.a --shared

Then we test the static call The c file of so file is compiled and linked with the command line exe

gcc F:\a_so\d_s_use.c -o F:\a_so\d_s_use.exe F:\a_so\libDynamic_lib.a F:\a_so\libDynamic_lib.so

Link the two required library files, both of which are indispensable. Finally, we get exe file, execute to get:

It looks as expected, but is it true?

In fact, we call A file is so C file, and this file has implemented these two functions, so in fact, we can't determine whether to call the content in a or Content in so

Although if you put it Removing so file will show the missing so file, but for the sake of preciseness, let's do it again

Our a at this time_ The so folder is very messy. Let's create a new tmp folder to store the rewritten files

So let's rewrite so C as n_so.c the contents are as follows:

__declspec(dllexport) int add(int a,int b){
    return a+b;    
}  
__declspec(dllexport) int multiplication(int a,int b){
    return a*b;
} 
__ declspec(dllexport) and__ Similar to declspec(dllimport), it is used for export and import is for import

Then we execute the gcc command:

gcc -shared -o F:\a_so\newtmp\n_so.so F:\a_so\newtmp\n_so.c -Wl,--out-implib,F:\a_so\newtmp\n_so_a.a

--Out implib is the import library lib file to create it, which is indispensable. Wl is actually an instruction with multiple parameters

Then we got it So and import library A file, and then we write a test file tst_so_s.c:
#include<stdio.h>
__declspec(dllimport) int add(int a,int b);
__declspec(dllimport) int multiplication(int a,int b);
int main() {
    printf("%d\n",add(5,6));
    printf("%d\n",multiplication(5,6));
    getchar(); 
    return 0; 
}
Then execute the command line
gcc F:\a_so\newtmp\tst_so_s.c -o F:\a_so\newtmp\tst_so_s.exe F:\a_so\newtmp\n_so_a.a F:\a_so\newtmp\n_so.so

The above command line is to process and output the c file as exe, link a import and storage So dynamic library, actually optical link a or So can pass, but not without explanation a the file is linked internally Contents of so file

Then we get an exe file, and the execution results are as follows:

 

For verification There is no implementation function in the a file. Let's put it Remove the so file and execute the exe file. You will get this result:

 

 

You can find the final a file is the implementation of calling the functions in so file,

Did a mirror image experiment and put it A remove the file and find that it can run. Description A file is for developers to use to get the function set

Careful friends may find me useless here h file, which is to simplify the initial logic. In fact, the principle is like this, if you write it h and c file, the first step of the compiler is precompiling, combining the two as i file output, some of our usual precompiles such as #define are also written in c file is not?


 

Posted by jandrews on Fri, 13 May 2022 07:53:16 +0300