gcc/g++ dynamic link library and static link library writing

Source: Internet
Author: User

function library is generally divided into static library and dynamic library two kinds.

Static libraries:

Refers to the compilation of links, the code of the library files are all added to the executable file, so the resulting file is larger, but at run time also no longer need the library file. The suffix is generally ". a".

Dynamic libraries:

Conversely, when you compile a link, the code for the library file is not added to the executable file, but the library is loaded by the runtime link file when the program executes, which saves the overhead of the system. The general suffix of the dynamic library is ". So", and gcc/g++ uses the dynamic library by default at compile time. Whether a static library or a dynamic library, it is created by an. o file.

Compilation of dynamic libraries:Here's an example of how to build a dynamic library. Build a header file: Dynamic.h, three. cpp files: dynamic_a.cpp, Dynamic_b.cpp, dynamic_c.cpp, we compile these files into a dynamic library: libdynamic.so. //dynamic.h
#ifndef __dynamic_h_ #define __dynamic_h_<iostream>void  dynamic_a (); void dynamic_b (); void dynamic_c ();
#endif

  

//dynamic_a.cpp:
#include"dynamic.h"void  dynamic_a () {  cout<<"  "<<Endl;}
 //dynamic_b.cpp:
#include"dynamic.h"void  dynamic_b () {  cout<<"  "<<
 //dynamic_c.cpp:
#include"dynamic.h"void  Dynamic_c () {  cout<<"  "<<Endl;}
 compile these files into a dynamic library libdynamic.so. The compile command is as follows: 
g++ dynamic_a.cpp dynamic_b.cpp Dynamic_c.cpp-fpic-shared-o libdynamic.so
  parameter Description:-shared: This option specifies that the dynamic connection library be generated -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.    in the above section, we have generated a libdynamic.so dynamic link library, now we use a program to invoke the dynamic link library. File name is : main.cpp//main.cpp:
#include"dynamic.h"int  main () {  dynamic_c ();  Dynamic_c ();  Dynamic_c ();   return 0 ;}

link main.cpp and libdynamic.so into an executable file, main. The command is as follows:
g++ main.cpp-l.-ldynamic-o Main

parameter Description:-L.: indicates the library to be connected in the current directory -ldynamic: 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   test the executable program if main is already linked to the dynamic library libdynamic.so, if Libdynamic.so is listed, then the normal link is indicated. You can execute the following command:
LDD Main

 If you run:
./main
error occurred : "Error while loading shared libraries:libdynamic.so:cannot Open Shared object file:no such file o R directory   cause of error :The LD hints that the library file cannot be found, and the library file is in the current directory.  The default directory for the linker ld is/lib and/usr/lib, and if you put it on a different path, you need to let LD know where the library file is. Workaround: Method 1: Edit the/etc/ld.so.conf file to include the directory where the library file is located in the new row, as the author should add: /home/neu/code/dynamic_library Run:
sudo ldconfig

The purpose is to load with Ldconfig to update the/etc/ld.so.cache file;   Method 2:Create a new file with a. conf suffix in the/etc/ld.so.conf.d/directory, and add the directory where the library file is located in the file;Run:
sudo ldconfig

To update the/etc/ld.so.cache file;

  The Ld.so.cache update is incremental, just like the path system environment variable, not re-established from scratch, but cumulatively. The Ld.so.cache file is created from scratch, unless the power is turned back on. Method 3:in the BASHRC or profile file is defined with Ld_library_path and then loaded with source. Method 4:you can directly use the compile link to tell the system where your library is . execute main To see if main is invoking a function in the dynamic-link library.
./main

  Compile the static library:

Readers can create their own code, the author is lazy, the above code to demonstrate, it is best to generate the dynamic library of all the things deleted.

1. Compile the static library:
g++-C dynamic_a.cpp dynamic_b.cpp Dynamic_c.cpp  

2. Create a static library file with the AR command (archive the target document)
AR cr libstatic.a dynamic_a.o dynamic_b.o DYNAMIC_C.O  //CR flag tells AR to encapsulate the object File (archive)

parameter Description:d Delete a file from the specified static library file
m move the file to the specified static library file
p Output The file specified in the static library file to standard output
Q quickly append files to the static library file
r Insert the file into the static library file
T displays a list of files in a static library file
x extracting files from a static library file
A adds a new target file (*.O) to the existing file in the static library file Use the nm-s command to view the contents of a. A file

3. Link Static library
g++ main.cpp-lstatic-l.-static -o main//the-static option here is to tell the compiler that Hello is a static library and can also be used

g++ main.cpp-lstatic-l. -O Main

execute the following command because the author still uses the code of the dynamic library, so the result is the same
./main

gcc/g++ dynamic link library and static link library writing

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.