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

C language programming gcc how to generate static libraries. An and dynamic libraries. So

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to generate static library.a and dynamic library.so about C language programming gcc, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Environment: Ubuntu Desktop 18.04

What are static and dynamic libraries?

We usually need to make some common functions into function libraries for other programs to use. Function libraries are divided into static libraries and dynamic libraries.

Static libraries are linked into the object code at compile time and are no longer needed at run time.

Dynamic libraries are not linked to the object code at compile time, but are loaded at runtime. In this way we can dynamically change certain functions of the program by changing the dynamic library.

Under Linux, ar tools are used to compress target files together, number and index them for easy lookup and retrieval.

Second, gcc generates.a static library and.so dynamic library 1. Generate static library (.a)1.1 Edit to generate example programs hello.h, hello.c and main.c

hello.h

#ifndef HELLO_H//If the source file is not defined, compile the following code #define HELLO_H//define macro void hello(const char *name);#endif/HELLO_H//end of ifndef

hello.c

#includevoid hello(const char *name){ printf("Hello %s!\ n",name);}

main.c

#include "hello.h"int main(){ hello("everyone"); return 0;}1.2 Compile hello.c to.o file

Both static and dynamic libraries are created from.o files. Therefore, we must compile the source code hello.c into a.o file through gcc, and use the command under the Linux system terminal.

gcc -c hello.c

To make sure we get the.o file, we can use ls

1.3 Create static libraries from.o files

The command specification for static library filenames is to prefix lib followed by the static library name with an extension of.a. For example, if we create a static library named hello, the static library filename would be libhello.a. To create a static library on Linux, you need to use the ar command. Enter the following command at the terminal.

ar -crv libmyhello.a hello.o

Similarly, we can use ls command to check the results.

1.4 Using static libraries in programs

The static library is complete, how to use its internal functions? You only need to include prototype declarations for these utilities in the source programs that use them, and then specify static library names when generating object files with the gcc command.

Method 1:

Enter the following command at the terminal:

gcc -o hello main.c -L. -lmyhello

For custom libraries, main.c can also be placed between-L. and-lmyhello, but not after them, otherwise myhello is not defined, but when it is a system library, such as g++ -o main (-L/usr/lib) -lpthread main.cpp, there will be no error.

Method 2:

gcc main.c libmyhello.a -o hello

Method 3:

Mr. Main.o

gcc -c main.c

Regenerate executable files:

gcc -o hello main.o libmyhello.a

1.5 Verify the characteristics of static libraries

Below we run the executable file with the static library removed and find that the program still runs normally, indicating that the static library has nothing to do with program execution. We delete the libmyhello.a file using the rm command, and then execute the hello program.

2. Generating Dynamic Libraries (.so)2.1 Creating Dynamic Library Files from.o Files

The dynamic library filename convention is similar to the static library filename convention, adding the prefix lib to the dynamic library name, but with the file extension.so. For example, we will create a dynamic library named myhello, and the dynamic library file name will be libmyhello.so. Enter the following command on the terminal to get the dynamic library file libmyhello.so.

gcc -shared -fPIC -o libmyhello.so hello.o

2.2 Using Dynamic Libraries in Programs

Using dynamic libraries in your program is the same as using static libraries. You include declarations of these public functions in the source program that uses them, and then specify the dynamic library name for compilation when generating the object file with the gcc command. Enter the following command at the terminal

gcc -o hello main.c -L. -lmyhello

Or you can use commands

gcc main.c libmyhello.so -o hello

There is no error at this point (if there is no libmyhello.so, there will be an error), but the next time you run the program, you will prompt an error, because although the connection is using the dynamic library of the current directory, the runtime will look for the library file in the/usr/lib directory. You can copy the file libmyhello.so to the directory/usr/lib so that it runs without errors.

Move the libmyhello.so file to the/usr/lib directory by typing the following command on the terminal

sudo mv libmyhello.so /usr/lib

As you can see, the execution was successful.

If both dynamic and static libraries exist, we can find out through experiments that dynamic libraries are preferred.

III. Example 1. Example 11.1 Code

The code is as follows:

A1.c

#includevoid print1(int arg){ printf("A1 print arg:%d\n",arg);}

A2.c

#includevoid print2(char *arg){ printf("A2 printf arg:%s\n",arg);}

A.h

#ifndef A_H#define A_Hvoid print1(int);void print2(char *);#endif

test.c

#include#include "A.h"int main(){ print1(1); print2("test"); exit(0);}

1.2 Generation and Use of Static Library.a File

The first step is to generate the.o file and enter the following command in the terminal

gcc -c A1.c A2.c

Next is to generate the static library.a file, enter the following command in the terminal

ar crv libafile.a A1.o A2.o

Finally, use the.a library file to create an executable program (PS: If you use this method, make sure that the generated.a file is saved in the same directory as the.c file, i.e., both in the current directory). Enter the following command at the terminal

gcc -o test test.c libafile.a./ test

1.3 Generation and Use of Dynamic Library.so File

The first is to generate the target file (.o), where the.o file must be generated by adding "-fpic"(small mode, less code), otherwise an error will be reported when generating the.so file. Enter the following command at the terminal

gcc -c -fpic A1.c A2.c

Next is to generate the shared library (dynamic library).so file

gcc -shared *.o -o libsofile.so

Create executable programs using.so library files

gcc -o test test.c libsofile.so./ test

This error is reported because Linux systems only look for.so files in/lib and/usr/lib directories, so you need to copy the corresponding.so file to the corresponding path. Enter the following command at the terminal

sudo cp libsofile.so /usr/lib

Then execute the test program, you can run successfully.

And it can be used directly.

gcc -o test test.c -L. -lname

to use the corresponding library file, where

-L. : means that path can be defined by yourself under the current directory, that is, -Lpath can be used.

-lname: name is the name of the corresponding library file (except lib), i.e. if libafile.a is used, name is afile; if libsofile.so is used, name is sofile.

2. Example 22.1 Code

sub1.c

float x2x(int x1,int x2){ return (float)(x1*x2);}

sub2.c

float x2y(int x1,int x2){ return (float)(x1)/x2;}

sub.h

#ifndef SUB_H#define SUB_Hfloat x2x(int x1,int x2);float x2y(int x1,int x2);#endif

main.c

#include#include "sub.h"int main(){ int x1,x2; scanf("%d %d",&x1,&x2); printf("x1*x2=%f\n",x2x(x1,x2)); printf("x1/x2=%f\n",x2y(x1,x2)); return 0;}2.2 Generation and use of static library.a files

Enter the following commands at the terminal in turn

gcc -c sub1.c sub2.car crv libsub.a sub1.o sub2.ogcc -o main main.c libsub.a./ main4 2

Check the file size by typing the following command in the terminal

du -h main

The size of the executable file generated at this point is 12k

2.3 Generation and Use of Dynamic Library.so File

Enter the following commands at the terminal in turn

gcc -shared -fpic -o libsub.so sub1.o sub2.osudo cp libsub.so /usr/libgcc -o main main.c libsub.so./ main4 2

Similarly, enter the command to query the size of main at the terminal

Although as large as the executable generated by the static library above, this is due to the simplicity of the function. For more complex file compilations, the size of the executable generated by a static library should theoretically be larger than the size of the executable generated by a dynamic library.

About "C language programming gcc how to generate static library.a and dynamic library.so" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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