Using gcc to generate static and dynamic libraries
Pangu pioneered the world. We write a program to output some content to the terminal. Inevitably, we need to use the system library. In the process of writing programs, we often encounter the process of using the library, whether system library or third-party library, which we collectively call lib library.
The links of libraries are divided into two kinds, static and dynamic libraries.
1. static library
Static libraries can be seen as a collection of object files, which may contain many functions. The most commonly used C language static library libc in linux is located in / usr/lib/libc.a. It consists of hundreds of C language programs, such as printf.o,scanf.o, fread.o,fwrite.o and so on. It is inconvenient to provide these scattered files to users, so people usually use ar compression program to compress these target files together, then number and index these files, and finally form the libc.a file.
Run the command to see what it contains, and then filter to see fread
$ ar -t /usr/lib/x86_64-linux-gnu/libc.a | grep fread iofread.o __freading.o __freadable.o iofread_u.o fread_chk.o fread_u_chk.o
If we want to find the printf.o file in it, we can use the command
$ objdump -t /usr/lib/x86_64-linux-gnu/libc.a | grep printf.o vfprintf.o: file format elf64-x86-64 vprintf.o: file format elf64-x86-64 reg-printf.o: file format elf64-x86-64 fprintf.o: file format elf64-x86-64 printf.o: file format elf64-x86-64 snprintf.o: file format elf64-x86-64 sprintf.o: file format elf64-x86-64 asprintf.o: file format elf64-x86-64 dprintf.o: file format elf64-x86-64 vfwprintf.o: file format elf64-x86-64 fxprintf.o: file format elf64-x86-64 iovsprintf.o: file format elf64-x86-64 fwprintf.o: file format elf64-x86-64 swprintf.o: file format elf64-x86-64 vwprintf.o: file format elf64-x86-64 wprintf.o: file format elf64-x86-64 vswprintf.o: file format elf64-x86-64 vasprintf.o: file format elf64-x86-64 iovdprintf.o: file format elf64-x86-64 vsnprintf.o: file format elf64-x86-64 obprintf.o: file format elf64-x86-64
You can see that the printf.o file is there.
Let’s take the helloworld.c program we wrote as an example.
#include int main(int argc, char *argv[])
$ gcc helloworld.c -o helloworld
It will be dynamically linked, so we can see the size of the generated file.
$ ls -l helloworld -rwxr-xr-x 1 gnc gnc 8304 10 Month 1115:06 helloworld
You can see that the size is 8303 bytes. We use the option — static to make gcc link statically:
$ gcc -static helloworld.c -o helloworld
$ ls -l helloworld -rwxr-xr-x 1 gnc gnc 844704 10 Month 1115:09 helloworld
You can see a significant increase in size.
2. dynamic library
The advantage of static linking is that after the linking is completed, the running program does not need the original support library, but the disadvantage is that the file size is large, and it is difficult to update.
So there are dynamic links, that is, do not link statically, but wait until the dynamic runtime to link. Symbol location of dynamic links occurs when the address space is allocated at runtime, and then the program is run.
Specific linking process is also more complex, we just need to know how to create such a dynamic library.
3. gcc creates static and dynamic libraries
/* add.h */ void setSummand(int summand); int add(int summand); /* add.c */ #include int gSummand; void setSummand(int summand) < gSummand = summand; >int add(int summand) < return gSummand + summand; >void __attribute__ ((constructor)) initLibrary(void) < // // Function that is called when the library is loaded // printf("Library is initialized\n"); gSummand = 0; >void __attribute__ ((destructor)) cleanUpLibrary(void) < // // Function that is called when the library is »closed«. // printf("Library is exited\n"); >
/* answer.h */ int answer(); /* answer.c */ #include "add.h" int answer() < setSummand(20); return add(22); // Will return 42 (=20+22) >
#include #include "add.h" #include "answer.h" int main(int argc, char* argv[])
Then we create directories. bin/static and. / bin/shared.
The directory structure after completion is as follows
$ tree . ├── add.c ├── add.h ├── answer.c ├── answer.h ├── bin │ ├── shared │ └── static └── main.c 3 directories, 5 files
We run commands to generate target files
$ gcc -c main.c -o bin/main.o # Static library $ gcc -c add.c -o bin/static/add.o $ gcc -c answer.c -o bin/static/answer.o # Dynamic library gcc -c -fPIC add.c -o bin/shared/add.o gcc -c -fPIC answer.c -o bin/shared/answer.o
3.1 Create static libraries
$ ar rcs bin/static/libtq84.a bin/static/add.o bin/static/answer.o
Package and compress the two target files into a static library file.
$ gcc bin/main.o -Lbin/static -ltq84 -o bin/static-main
$ ./bin/static-main Library is initialized 5 + 7 = 12 And the answer is: 42 Library is exited
3.2 Creating Dynamic Library
$ gcc -shared bin/shared/add.o bin/shared/answer.o -o bin/shared/libtq84.so
$ gcc bin/main.o -Lbin/shared -ltq84 -o bin/use-shared-library
$ ./bin/use-shared-library ./bin/use-shared-library: error while loading shared libraries: libtq84.so: cannot open shared object file: No such file or directory
We will be prompted that we cannot find the dynamic link library. We need to modify the LD_LIBRARY_PATH variable to point to our dynamic library path.
export LD_LIBRARY_PATH=$(pwd)/bin/shared
./bin/use-shared-library Library is initialized 5 + 7 = 12 And the answer is: 42 Library is exited
Yes, of course, you can also choose to place the dynamic library under the path / usr/lib and run the following commands
sudo mv bin/shared/libtq84.so /usr/lib sudo chmod 755 /usr/lib/libtq84.so
Posted on Fri, 11 Oct 2019 04:08:36 -0400 by gamerzfuse
Hot Tags
- Java — 7906
- Database — 3176
- Python — 3103
- Attribute — 2963
- Programming — 2938
- Javascript — 2788
- Spring — 2575
- xml — 2270
- Android — 2243
- Linux — 2204
- JSON — 2150
- less — 2137
- network — 2115
- github — 2063
- MySQL — 1760
- SQL — 1616
- PHP — 1559
- encoding — 1360
- Mobile — 1172
- Apache — 1137
C++ Dynamic Shared Library on Linux
This is a follow-up to Dynamic Shared Library compilation with g++. I’m trying to create a shared class library in C++ on Linux. I’m able to get the library to compile, and I can call some of the (non-class) functions using the tutorials that I found here and here. My problems start when I try to use the classes that are defined in the library. The second tutorial that I linked to shows how to load the symbols for creating objects of the classes defined in the library, but stops short of using those objects to get any work done. Does anyone know of a more complete tutorial for creating shared C++ class libraries that also shows how to use those classes in a separate executable? A very simple tutorial that shows object creation, use (simple getters and setters would be fine), and deletion would be fantastic. A link or a reference to some open source code that illustrates the use of a shared class library would be equally good. Although the answers from codelogic and nimrodm do work, I just wanted to add that I picked up a copy of Beginning Linux Programming since asking this question, and its first chapter has example C code and good explanations for creating and using both static and shared libraries. These examples are available through Google Book Search in an older edition of that book.
I’m not sure I understand what you mean by «using» it, once a pointer to the object is returned, you could use it like you use any other pointer to an object.
The article I linked to shows how to create a function pointer to an object factory function using dlsym. It doesn’t show the syntax for creating and using objects from the library.
You will need the header file describing the class. Why do you think you have to use «dlsym» instead of just letting the OS find and link the library at load time? Let me know if you need a simple example.
@nimrodm: What’s the alternative to using «dlsym»? I’m (supposed to be) writing 3 C++ programs that will all use the classes defined in the shared library. I also have 1 Perl script that will use it, but that’s a whole other problem for next week.