Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the process of C language linking?

Shulou Source: shulou.com Published: 2022-05-31 21:04:36 09月21日 Update

Most people do not understand the knowledge points of this article "what is the process of linking in C language?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "what is the process of C language linking" article.

I. the significance of the linker

The main function of the connector is to deal with the referenced parts of each module, so that the modules can be connected correctly.

II. Module links

Static link

The linker adds the contents of the library directly to the executable program when linking

Creation and use of static Library under Linux

Compile static library source code: gcc-c lib.c-o lib.o

Generate static library file: ar-Q lib.a lib.o

Compile with static libraries: gcc main.c lib.a-o main.out

Let's take a look at the code for a static link example:

Slib.c

Char* name () {return "Static Lib";} int add (int a, int b) {return a + b;}

Test.c

# include extern char* name (); extern int add (int a, int b); int main () {printf ("Name:% s\ n", name ()); printf ("Result:% d\ n", add (2,3)); return 0;}

Type gcc-c slib.c-o slib.o to compile the static library source code:

Type ar-Q slib.a slib.o to generate a static library file:

Type gcc Test.c slib.a-o Test.out, compile using the static library, and generate the .out file:

Then type. / Test.out, and you can run it, as follows:

If you delete all the slib.o,slib.a files, run. / Test.out, and find that it works properly, that is, the .o files and .a files mentioned above are completely linked into the executable program, and the execution of the executable program has nothing to do with .o files and .a files.

Dynamic link

The executable program dynamically loads the library to link at run time.

The contents of the library will not go into the executable program

Creation and use of dynamic Library under Linux

Compile dynamic library source code: gcc-shared dlib.c-o dlib.so

Compile with dynamic libraries: gcc main.c-ldl-o main.out

Critical system call

Dlopen: opening dynamic library files

Dlsym: find the function in the dynamic library and return the calling address.

Dlclose: closing dynamic library files

Let's look at an example of a dynamic link:

Dlib.c

Char* name () {return "Dynamic Lib";} int add (int a, int b) {return a + b;}

Demo.c

# include # include int main () {void* pdlib = dlopen (". / dlib.so", RTLD_LAZY); char* (* pname) (); int (* padd) (int, int); if (pdlib! = NULL) {pname = dlsym (pdlib, "name"); padd = dlsym (pdlib, "add") If ((pname! = NULL) & & (padd! = NULL) {printf ("Name:% s\ n", pname ()); printf ("Result:% d\ n", padd (2,3));} dlclose (pdlib);} else {printf ("Cannot open lib.\ n");} return 0;}

First type gcc-shared dlib.c-o dlib.so to compile the dynamic library source code:

Type gcc Demo.c-ldl-o Demo.out, compile using the dynamic library, and generate the .out file:

Then type. / Demo.out, and you can run it, as follows:

If you delete dlib.so, the operation will report an error:

So the library file dlib.so is dynamically loaded into memory during the run-time of the program, which is the difference from static links.

The above is the content of this article on "what is the process of linking in C language". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Link dynamic static compile run content file input program library file source code generation language process that is modules between articles knowledge examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft MySQL vpn Docker