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 environmentUnder 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: //myAPI.h
int ADD(int a, int b);
int MINUS(int a, int b);
//myAPI.cpp
#include "myAPI.h"
int ADD(int a, int b){
return a + b;
}
int MINUS(int a, int b){
return a - b;
}
//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; }
Finally, first compile our myAPI.cpp file to generate myAPI. o Target file
g++ -c myAPI.cpp
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
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
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
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
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
"-L." mark tells G++ that the function library may be located in the current directory; use "-lmyAPI ” mark to tell the G++
driverprogram 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
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!