Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the process of C language linking?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report