"Go" gcc compilation using dynamic link library

Source: Internet
Author: User

relevant GCC parameters:-l-l-shared -fpic -static-c-o Original Address: "Script House" http://www.jb51.net/article/34990.htmDepending on the link period, the library also has static library and dynamic library points, different from the static library, the dynamic Library link is at the time of execution of the program is linked

1 Classification of libraries

Depending on the link period, the library also has a static library and a dynamic library of points.

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

Unlike static libraries, links to dynamic libraries are linked when the program is executed. So, even after the program compiles, the library must remain on the system for the program to run. (TODO: What the link stage does when linking to a dynamic library)

2 comparison of static and dynamic libraries

The link static library is actually a kind of paste copy in a sense, except that it operates on the object code and not the source. Because the static library is linked to the library directly embedded in the executable file, there are two problems.

The first is that the system space is wasted. This is obvious, imagine that if more than one program links the same library, then each generated executable will have a copy of the library, will inevitably waste system space.

Moreover, err, even the well-debugged library, will inevitably be wrong. Once you find a bug in the library, it's a bit of a hassle to save. The program that links the library must be found and then recompiled.

The emergence of dynamic libraries is making up for the disadvantages of static libraries. Because the dynamic library is linked when the program is running, it saves disk space by keeping only one copy on the disk. If you find a bug or want to upgrade it is also very simple, just use the new library to replace the original is OK.

So, is the static library useless?

Answer Yue: not also not also. There is no saying: the existence is reasonable. Since the static library is not lost in the long history of the river, it must have its application. Imagine this situation: if you use the Libpcap library to compile a program, to be run, and his system is not installed Pcap library, how to solve it? The simplest way is to compile the program by linking all the libraries you want to link to their static libraries, so that you can run the program directly on someone else's system.

The so-called win will be lost, because the dynamic library in the program is linked to run, so the speed of the program and link to the static version of the library will inevitably be discounted. However his flaws, the lack of a dynamic library relative to its benefits in today's hardware is simply negligible, so the linker is generally a priority link to the dynamic library when linking, unless you specify the link static library with the-static parameter.

Dynamic Link Library

1. Create a dynamic link library

The code is as follows:
#include <stdio.h>
void Hello ()
{
printf ("Hello world/n");
}

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

"Reprint Note: So is the suffix of the dynamic link library under Linux, the LD is sensitive to this suffix, and cannot be changed to other suffixes such as DLLs." Also because Gcc-c does not add-o to specify the file name, regardless of other parameters, Generates a XXXXX.O file, you must specify a file name named Xxxx.so with-o when compiling the link library. 】

2. Re-edit a test file test.c, which reads as follows

The code is as follows:
#include <stdio.h>
int main ()
{
printf ("Call Hello ()");
Hello ();
}

Compile GCC Test.c-lhello (reprint note:-lxxxxx There is no space in the middle, is to tell GCC to find libxxxxx.so this library, and libxxxxx.so filename is gcc to generate, is a fixed format, can not be changed. )
The-l option tells the compiler to use the Hello Library. The strange thing is that the name of the dynamic library is libhello.so, but this uses hello.
But this doesn't work, and the compilation goes wrong.

In function ' main ':
TEST.C: (. text+0x1d): Undefined reference to ' hello '
Collect2:ld returned 1 exit status
This is because the Hello Library is in our own path and the compiler cannot find it.
You 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 find the library file in the current directory

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

I can't find the library.

There are two ways of doing this:

First, you can add the current path to/etc/ld.so.conf and then run Ldconfig, or run ldconfig with the current path as a parameter (to have root permission).

Second, add the current path to the environment variable Ld_library_path

Of course, if you feel that it will not cause confusion, you can directly copy the library into the/lib,/usr/lib/and other places (there is no way to avoid, so also have permission), so that the linker and the loader can find the library accurately.

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

Let's talk about the static link library.

Still use the hello.c and test.c just now.
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 mate parameter-R creates a new library LIBHELLO.A and inserts the object files listed in the command line. In this way, if the library does not exist, the parameter-R will create a new library, and if the library exists, it will replace the original module with the new one.
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 generated hello.static is no longer dependent on LIBHELLO.A.

Two useful commands

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

In addition, you can 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 (as specified by the command line parameters), if the target program does not have a link to the dynamic library, print "Not a dynamic executable", LDD use refer to manpage.

"Go" gcc compilation using dynamic link library

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.