Home > Operation and Maintenance > Linux Operation and Maintenance > Detailed explanation of g++ compilation and use of static and dynamic libraries under Linux

Detailed explanation of g++ compilation and use of static and dynamic libraries under Linux

黄舟
Release: 2017-05-28 11:32:40
Original
4695 people have browsed it

The following editor will bring you an articleLinuxCompilation and use of g++ under LinuxstaticLibrary and dynamic library methods. The editor thinks it is quite good, so I will share it with you now and give it a reference. Let’s follow the editor and take a look.

windows environment

Under the project, we usually develop C++ projects in IDEs such as VS. We may be familiar with generating and using static libraries (*.lib) and dynamic libraries (*.dll). However, In the Linux environment, there is another set of modes. The corresponding static libraries (*.a) and dynamic libraries (*.so) are generated and used in different ways. You may not be comfortable with it at first, but you should use it more. You will get used to this kind of use, because the steps are not as complicated as configuring under VS.

The following is a summary of the methods of generating and using static libraries and dynamic libraries under Linux: (Since it is a C++ project, So the compiler uses g++, but it is the same as the use of gcc)

First of all,

preparation work

, we need to encapsulate the functions into library files The header file and source file are written as follows:

Next, prepare a main function source file for testing:

//main.cpp 
#include "myAPI.h" 
#include <iostream> 
 
int main(){ 
  std::cout << "1 + 1 = " << ADD(1, 1) << std::endl; 
  std::cout << "1 - 1 = " << MINUS(1, 1) << std::endl; 
  return 0; 
}
Copy after login

Finally, first compile our myAPI.cpp file to generate myAPI. o Target file

g++ -c myAPI.cpp
Copy after login

1. Generate a static library and useUse the command ar to generate a static library under Linux to process the myAPI.o file generation For static library files, the generated library files should follow the specifications, and the library files under Linux should be prefixed with "lib". Therefore, for this example, the libmyAPI.a static library can be generated through the following command:

ar crv libmyAPI.a myAPI.o
Copy after login

Next. You can use the static library during the project compilation process. At this time, the definition file of the library function myAPI.cpp is no longer needed. The main.cpp compilation command is as follows (note that the dependent static library file must be placed after the dependent item). :

g++ main.cpp libmyAPI.a -o output
Copy after login

After the compilation is passed, the executable file output can be run. At this time, libmyAPI.a is no longer needed. Execute the command and the output result is as follows:

./output
Copy after login
Copy after login

2. Generate a dynamic library and use When compiling under Linux, you can use the -shared parameter to generate a dynamic library (.so) file, as follows

g++ -shared -fPIC -o libmyAPI.so myAPI.o
Copy after login

The generated dynamic library needs to be declared at compile time and needs to be depended on at runtime. The declaration is as follows

g++ main.cpp -L. -lmyAPI -o output
Copy after login

"-L." mark tells G++ that the function library may be located in the current directory; use "-lmyAPI ” mark to tell the G++

driver

program to reference the shared function library libmyAPI.so during the connection phase. If the following error is prompted when using it, just move libmyAPI.so to the /usr/lib directory:

./output
Copy after login
Copy after login

The above is the detailed content of Detailed explanation of g++ compilation and use of static and dynamic libraries under Linux. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template