GCC compile dynamic and static link libraries

Source: Internet
Author: User
Tags naming convention

We usually make a function library of some common functions for other programs to use. function library is divided into two kinds: static library and dynamic library. The static library is connected to the target code when the program compiles, and the static library is no longer needed when the program runs. Dynamic libraries are not connected to the target code when the program is compiled, but are loaded only when the program is run, so you also need a dynamic library presence when the program is running. This article is mainly to illustrate how to create static libraries and dynamic libraries in Linux, and to use them.

1th Step : Editors get examples of programs--hello.c and test.c;

The test program test.c called the public function my_lib_function.

hello.c:

#include <stdio.h>

void My_lib_function ()

{

printf ("Library routine called/n");

}

test.c:

int main ()

{

My_lib_function ();

return 0;

}

2nd Step : Compile hello.c into an. o file;

Both the static library and the dynamic library are created by the. o File (dynamic libraries can be passed directly through. c). Therefore, we must first compile the source program hello.c through GCC into an. o file.

At the system prompt, type the following command to get the hello.o file.

# gcc-c HELLO.C

3rd Step : Create a static library from the. o file;

The naming convention for static library file names is prefixed with Lib, followed by a static library name, with the extension. A.

For example, we will create a static library named Myhello, then the static library file name is LIBMYHELLO.A. You need to be aware of this when creating and using static libraries. Create a static library with the AR command.

Type the following command at the system prompt to create the static library file LIBMYHELLO.A.

# ar CRV libmyhello.a hello.o

4th Step : Use the static library in the program;

The static library is finished, how to use its internal functions. You only need a prototype declaration that contains these common functions in the source program that uses them, and then indicates the static library name when the target file is generated with the GCC command, and GCC will connect the common function to the destination file from the static library. Note that GCC appends the static library name with the prefix lib, and then append the file name of the static library that the extension. A finds to the static library file.

# Gcc-o Test Test.c-l.-lmyhello

#./test

Library routine called

We delete the static library file try the common function Hello is really connected to the target file hello.

# RM LIBMYHELLO.A

#./test

Library routine called

The program runs as usual, and the common functions in the static library are already connected to the destination file.

Let's continue to see how to create a dynamic library in Linux. We're still starting with the. o file.

5th Step : Create a dynamic library file from the. o file;

The dynamic library file name specification is similar to the static library file name specification, and also adds prefix lib to the dynamic library name, but its file name extension is. So.

For example, we will create a dynamic library named Myhello, then the dynamic library file name is libmyhello.so. Use GCC to create a dynamic library.

At the system prompt, type the following command to get the dynamic library file libmyhello.so.

# Gcc-shared-fpci-o libmyhello.so hello.o

You can also create a dynamic library directly from the. c File: Gcc-shared-fpci-o libmyhello.so hello.c

We still use the LS command to see if the dynamic library file is generated.

6th Step : Use dynamic library in the program;

Using dynamic libraries in your programs is exactly the same as using a static library, and it is also a prototype declaration that contains these common functions in the source program that uses them, and then indicates that the dynamic library name is compiled when the target file is generated with the GCC command. We first run the GCC command to generate the target file, and then run it to see the results.

# Gcc-o Test Test.c-l.-lmyhello

#./test

./test:error while loading shared libraries:libmyhello.so:cannot open Shared object file:no such file or directory

Oh. There was a mistake. Quick look at the error prompts, the original is not found dynamic library file libmyhello.so. When the program is running, it looks for the required dynamic library files in directories such as/usr/lib and/lib. If found, load the dynamic library, or you will be prompted to terminate the program like the above error. We copy the file libmyhello.so to the directory/usr/lib and try again.

# MV Libmyhello.so/usr/lib

#./test

Library routine called

It worked. This further illustrates that dynamic libraries are needed when the program is running.

If this occurs: Error while loading shared Libraries:libmyhello.so:cannot restore segment prot after reloc:permission denied error reason Access to this shared library has been disabled in SELinux

Solution: Close SELinux

1, edit/etc/selinux/config file, find selinux=enforcing, change to selinux=disabled

2, edit/etc/sysconfig/selinux file, find selinux=enforcing, change to selinux=disabled

3, restart the computer

7th step: continue;

We looked back and found that using the static library and the GCC command used to compile the target program using dynamic libraries is exactly the same as the GCC command uses when the static library and the dynamic library have the same name. Hold on to the problem will be the end of the mood, to try.

Delete other files first, leaving only hello.c test.c hello.o

# rm-f Test/usr/lib/libmyhello.so

To create the static library file libmyhello.a and the dynamic library file libmyhello.so.

# ar CR libmyhello.a hello.o

# Gcc–shared–fpci-o libmyhello.so hello.o

# ls

hello.c hello.o libmyhello.a libmyhello.so test.c

Through the last ls command above, you can find that both the static library file libmyhello.a and the dynamic library file libmyhello.so have been generated and are in the current directory.

# Gcc-o Test Test.c-l.-lmyhello

#./test

./test:error while loading shared libraries:libmyhello.so:cannot open Shared object file:no such file or directory

It is easy to know from the results of the program hello that when a static library and a dynamic library have the same name, the GCC command will use the dynamic library as a priority.

Note:


Compilation parameter parsing
The main thing is an option for the GCC command line:
-shared This option specifies that a dynamic connection library is generated (which allows the connector to generate an export symbol table of type T, and sometimes a weakly connected W-type export symbol) without which the external program cannot connect. Equivalent to an executable file
-fpic: For code that is compiled as location independent, the compiled code is location-dependent without this option, so dynamic loading is a way to copy the code to meet the needs of different processes, but not the purpose of real code segment sharing.
-L.: Indicates the library to connect to in the current directory
-ltest: When the compiler looks for a dynamic connection library, there is an implied naming rule, that is, precede the given name with Lib, followed by. So to determine the name of the library
Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.
Of course, if you have root permissions, you can modify the/etc/ld.so.conf file, and then call/sbin/ldconfig to achieve the same purpose, but if you do not have root permissions, then only the output Ld_library_path method.

When you call a dynamic library, there are a few problems that are often encountered, sometimes, it is clear that the library's header file directory through the "-I" included, the library file is guided by the "-L" parameter, and specified the "-L" library name, but through the LDD command to see if you can not find the link you specify the so file , all you have to do is modify the Ld_library_path or/etc/ld.so.conf file to specify the directory of the Dynamic library. This is usually done to solve the problem that the library cannot be linked.

Participation from: http://hi.baidu.com/cayaca/blog/item/1beba5e74282e925b9382003.html

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.