Go Using gcc/g++ to generate static and dynamic libraries under Linux (Z)

Source: Internet
Author: User
Tags naming convention

using gcc/g++ to generate static and dynamic libraries under Linux (Z)

2012-07-24 16:45:10|  Category: Linux | Tags: link library linux g++ gcc | Report | Font size Subscription

Under Linux, library files are generally placed under/usr/lib and/lib,

The name of the static library is generally libxxxx.a, where xxxx is the name of the Lib

The name of the dynamic library is generally libxxxx.so.major.minor, XXXX is the name of the Lib, major is the major version number, minor is the sub-version number

LDD command to view a shared library that an executable program relies on

This article focuses on how to create static and dynamic libraries in Linux and use them as an example:

Hello.h is the header file for the function library.

Hello.cpp is the source program for the library, which contains the public function hello, which will output "Hello world!" on the screen.

Main.cpp is the main program for the test library file, and the public function Hello is called in the main program.

Hello.h

#ifndef _hello_h_
#define _hello_h_

void Funcoutput ();

#endif

Hello.cpp

#include <stdio.h>

#include "hello.h"

void Funcoutput ()

{
printf ("%s", "Hello world!\n");

}

Main.cpp

#include "hello.h"

int main ()

{
Funcoutput ();
return 0;

}

We have three ideas at this time:

1) directly synthesize the target code into an. o file by compiling multiple source files.

2) by creating a static link library libmyhello.a, you can invoke the static link library when the main function calls the Hello function.

3) You can call the static link library when the main function calls the Hello function by creating the dynamic link library libmyhello.so.

2.3 Idea One: compiling multiple source files

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

# g++-C hello.cpp

Why not apply G++–o Hello Hello.cpp That's what we've said before, what do you mean by using-c? This involves the common sense of the g++ compilation option.

The g++–o that we typically use is to compile the. cpp source file into an executable binary code, which includes calling the external GNU assembler and the connector that is part of the true C compiler (CCL) in GCC and the actual executable code in the output that calls the GNU C compiler. With.

The g++–c is the end when the source file is converted to the target code using the GNU assembler, in which case the connector is not executed, so the output destination file will not contain the included information necessary to be loaded and executed as a Linux program, but it can be connected to a program at a later time.

We run the LS command to see if the hello.o file survived.

# ls

Hello.cpp hello.h hello.o main.cpp

In the results of the LS command, we see the hello.o file, which is done in this step.

Similarly compiled main

#g ++–c main.cpp

Link two files to an. o file.

#g ++–o Hello hello.o main.o

Run

#./hello

Hello everyone!

Complete ^ ^!

2.4 Idea two: Static link library

Let's start by looking at how to create a static library and use it.

The naming convention for Static library filenames is prefixed with lib, followed by a static library name with a. a extension. 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.

At the system prompt, type the following command to create a static library file, Libmyhello.a.

# ar CR libmyhello.a hello.o

We also run the LS command to see the results:

# ls

Hello.cpp hello.h hello.o libmyhello.a main.cpp

There are libmyhello.a in the results of the LS command.

The static library is finished, how to use its internal function? You only need to include a prototype declaration of these common functions in the source program that uses these common functions, and then specify the static library name when you generate the target file with the GCC command, and GCC will connect the public function to the destination file from the static library. Notice that GCC adds the prefix lib to the static library name and then appends the extension. A to the static library file name to find the static library files.

In program 3:main.cpp, we include the header file hello.h of the static library, and then call the public function hello directly in main program main. The following Mr. into the target program Hello, then run the Hello program to see how the results.

# g++-o hello main.cpp-l.-lmyhello

#./hello

Hello everyone!

Let's delete the static library file and try the common function hello whether it is really connected to the target file hello.

# RM LIBMYHELLO.A

Rm:remove regular file ' libmyhello.a '? Y

#./hello

Hello everyone!

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

One drawback of static-link libraries is that if we run many programs at the same time, and they use the same library function, we copy the same library function in memory in large numbers. In this way, a lot of precious memory and storage space will be wasted. Linux with a shared link library can avoid this problem.

shared libraries and static functions are in the same place, but the suffixes are different. For example, in a typical Linux system, the standard shared sequence function library is/usr/lib/libm.so.

When a program uses a shared function library, it does not connect the function code in the connection phase, but only a reference to the link function. When the final function import memory begins to actually execute, the function reference is parsed and the code of the shared function library is actually imported into memory. In this way, the functions of the shared link library can be shared by many programs at the same time, and can be stored only once. Another advantage of sharing a library is that it can be updated independently, with no effect on the function that calls it.

2.5 Ideas three, dynamic link library (Shared function library)

Let's continue to look at how to create a dynamic library in Linux. Let's start with the. o file.

The dynamic library file name specification is similar to the static library file name naming specification and also adds the 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 g++ to create a dynamic library.

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

# g++-shared-fpci-o libmyhello.so hello.o

The "PCI" command-line tag tells GCC that the generated code does not include a reference to the specific memory location of the function and variable, because it is not yet known which memory address space the application using the message code will connect to. The compiled hello.o can be used to create a shared link library. To create a shared link library, you only need to use GCC's "-shared" tag.

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

# ls

Hello.cpp hello.h hello.o libmyhello.so main.cpp

Call the dynamic-link library to compile the target file.

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

# g++-o hello main.cpp-l.-lmyhello

{You can also do this: g++-o hello main.cpp/home/zhdr/test/libmyhello.so without mv Libmyhello.so/usr/lib.

But you will also encounter the following problems, also need: Chcon-t texrel_shlib_t/home/zhdr/test/libmyhello.so}

Use the "-lmyhello" flag to tell the GCC driver to reference the shared function library libmyhello.so during the connection phase. The "-L." Flag tells the GCC library that it might be in the current directory. Otherwise, the GNU connector will look for the standard system functions directory.

#./hello

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

#

Error prompt, unable to find the dynamic library file libmyhello.so. At run time, the program looks for the required dynamic library files in directories such as/usr/lib and/lib. If found, loads the dynamic library, otherwise it will prompt similar errors to terminate the program. We'll copy the file libmyhello.so to the directory/usr/lib and try again. (This method is generally not recommended, preferably with the method of modifying LD)

# MV Libmyhello.so/usr/lib

#./hello

Hello everyone!

I did not succeed after this step, the error content is as follows:/hello:error while loading shared libraries:/usr/lib/libmyhello.so:cannot Restore segment Prot after Reloc:permission denied

Workaround (First determine if you are the root user, if not, the following two workarounds are useless):

1.
Chcon-t texrel_shlib_t/usr/lib/libmyhello.so
(Chcon-t texrel_shlib_t "You cannot share the absolute path of the library")

2.
#vi/etc/sysconfig/selinux File
or with
#gedit/etc/sysconfig/selinux File
Modify Selinux=disabled
Restart

}

Compile parameter parsing:
The main thing is an option for the GCC command line:
-shared This option specifies to generate a dynamic connection library (let the connector generate the export symbol table of type T, and sometimes the export symbol of the weakly connected W type) without which the external program cannot connect. Equivalent to an executable file
L-fpic: represents compiled to a location-independent code, without this option, the compiled code is location-dependent, so dynamic loading is a way of copying code to meet the needs of different processes, but not to achieve the purpose of real code segment sharing.
L-l.: Indicates the library to be connected in the current directory
L-ltest: The compiler has an implicit naming convention when looking for dynamic connection libraries, which is to precede the given name with Lib, followed by. So to determine the name of the library
L Ld_library_path: This environment variable indicates that the dynamic connector can load the path of the dynamic library.
L 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 goal, but if you do not have root privileges, you can only use the output Ld_library_path method.

Call the dynamic library when there are several problems often encountered, and sometimes, clearly has the library's header file in the directory through the "-I" include, the library is located in the file through the "-l" parameter boot, and specified the "-L" library name, but through the LDD command to see, it is dead and alive can not find your designated link , you have to specify the directory of the dynamic library by modifying the Ld_library_path or/etc/ld.so.conf file. This is usually done to solve the problem where the library cannot be linked.

Search path order when static library links:
1. LD will go to the parameters in the GCC command-l
2. Re-search for GCC environment variables Library_path
3. Find the default directory/lib/usr/lib/usr/local/lib This is the original compile GCC when written in the program

Dynamic Link-time, execution-time search path order:
1. The dynamic library search path specified when compiling the target code;
2. Environment variable LD_LIBRARY_PATH the specified dynamic library search path;
3. The dynamic library search path specified in the configuration file/etc/ld.so.conf;
4. The default dynamic library search path/lib;
5. The default dynamic library search path/usr/lib.

About environment variables:
Library_path environment variable: Specifies the program static link library file search path
LD_LIBRARY_PATH environment variable: Specifies the program dynamic link library file search path

Reference: http://blog.163.com/[email protected]/blog/static/130245975201151552938133/

[Turn]linux to generate a static and dynamic library with gcc/g++ (Z)

Related Article

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.