GCC compiling methods for using dynamic link libraries and static link libraries _c language

Source: Internet
Author: User

1 Classification of libraries

Depending on the link period, the library is divided into static and dynamic libraries.

The static library is linked in the link phase (as if it were nonsense, but this is the case), so the resulting executable file is not affected by the library, and even if the library is deleted, the program can still run successfully.

Unlike static libraries, dynamic library links are linked at the time the program is executed. So, even if the program is finished compiling, the library must remain on the system for the program to be invoked when it is run. (TODO: What does the link phase do when linking a dynamic library?)

2 comparison of static and dynamic libraries

Link Static library In a sense is also a kind of paste copy, but it is the object of the operation of the target code rather than the source. Since the static library is linked, the library is embedded directly into the executable file, which creates two problems.

The first is that the system space is wasted. It is obvious to imagine that if multiple programs link to the same library, then each generated executable will have a copy of the library, which will inevitably waste the system space.

Moreover, err, even if the library is carefully debugged, it will inevitably be wrong. Once you find a bug in the library, it's more troublesome to save it. You must find the program that links the library and recompile it.

And the emergence of dynamic library is to make up for the static library of the above drawbacks. Because dynamic libraries are linked while the program is running, disk space is saved by keeping only one copy of the disk. If you find a bug or want to upgrade it is very simple, just use the new library to replace the original line.

So, is not the static library is useless?

Answer Yue: not also not also. There is no saying: existence is reasonable. Static library Since there is no annihilation in the surging history of the river, it must have its application. Imagine such a situation: if you use Libpcap library to make a program, to be run by people, and his system does not install Pcap library, how to solve it? The easiest way to do this is to link all the libraries you want to link to their static libraries when you compile the program, so that you can run the program directly on someone else's system.

The so-called gain must be lost, because the dynamic library in the program is linked to the runtime, so the speed of the program and the link to the static library version is bound to discount. However, afterward, the lack of dynamic libraries relative to its benefits in the current hardware is simply negligible, so the linker in the link is generally preferred to link the dynamic library, unless the-static parameter to specify the link static library.

Dynamic Link Library

1. Create a dynamic link library

Copy Code code as follows:

#include <stdio.h>
void Hello ()
{
printf ("Hello world/n");
}

Compile the Hello.c-o libhello.so with the command gcc-shared into a dynamic library. As you can see, there is one more file libhello.so in the current directory.

2. Re-edit a test file test.c, the contents are as follows

Copy Code code as follows:

#include <stdio.h>
int main ()
{
printf ("Call Hello ()");
Hello ();
}

Compiling GCC Test.c-lhello
The-l option tells the compiler to use the Hello Library. The strange place is that the name of the dynamic library is libhello.so, where Hello is used.
But this does not work, the compilation will be wrong.

In function ' main ':
TEST.C: (. text+0x1d): Undefined reference to ' hello '
Collect2:ld returned 1 exit status
This is because the library of Hello is in our own path and the compiler cannot find it.
Need to use the-l option to tell the location of the Hello Library
GCC test.c-lhello-l. O Test
-L tell the compiler to look for a library file in the current directory

3. Execute after successful compilation./test, Still wrong

Said you couldn't find the library.

There are two ways of doing this:

You can add the current path to the/etc/ld.so.conf and then run Ldconfig, or run ldconfig with the current path as the parameter (with root permissions only).

Second, the current path into the environment variable Ld_library_path

Of course, if you think it will not cause confusion, you can directly copy the library into the/lib,/usr/lib/and other places (unavoidable, do also have permission), so that the linker and the loader can be accurate to find the library.

We use the second method:
Export ld_library_path=.: $LD _library_path
In this way, the second execution is successful.

Let's talk about the static link library

Still use just the hello.c and test.c.
1. gcc-c hello.c Note that the-shared option is not used here
2. Archive the target file ar-r libhello.a hello.o
Program AR fit parameter-R create a new library LIBHELLO.A and insert the object files listed in the command line. In this way, if the library does not exist, parameter-R will create a new library, and if the library exists, the original module will be replaced with the new module.
3. Link the static library in the program
GCC test.c-lhello-l.-static-o hello.static
or GCC test.c libhello.a-l.-O hello.static

The resulting hello.static are no longer dependent on LIBHELLO.A.

Two useful commands

File program is used to determine the type of files, under the File command, all the files will be true to their true colours.
By the way, a trick. Sometimes in Windows under the browser to download tar.gz or tar.bz2 files, suffix name will become strange Tar.tar, to Linux some novice do not know how to unpack. But the file type under Linux is not affected by the file suffix name, so we can use the command file Xxx.tar.tar to look at the file type and then extract the appropriate parameters with tar.

In addition, can also use the program LDD utility to judge.
LDD is used to print information about all the dynamic libraries that are linked by the target program (specified by the command-line arguments), if the target program does not have a linked dynamic library, print "Not a dynamical executable", LDD use refer to manpage.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.