For example, I have A.cpp, A.h, main.cpp three files.
To compile a linked C + + code:
First step: g++-C A.cpp main.cpp
This allows you to compile A.cpp and main.cpp code generation A.O and MAIN.O files "because A.cpp contains A.h header files, so you can only select CPP files at compile time."
Step two: g++-o test a.o MAIN.O
This allows you to link the compiled. o file into an executable file. The parameter after-O is the name of the compiled executable. Of course, you can also skip the first step, directly "g++-O test A.cpp main.cpp" once compiled and linked to the delivery of executable files.
Step three:./test
This allows you to run the executable file and execute A.cpp and main.cpp code.
Build and use a static library.
First step: g++-C A.cpp
Generate. o Files
Step two: AR-CR liba.a A.O
To generate a static library file, the parameter following the-CR is the name of the library file
Step three: g++-o test main.cpp liba.a
Using main.cpp and static libraries to generate executable files
Fourth step:./test
Running an executable file
Build and use dynamic libraries.
First step: g++-C A.cpp
Generate. o Files
Step two: g++-shared-fpic-o liba.so A.O
Generating Dynamic Library files
Step three: g++-o test main.cpp liba.so
Using main.cpp and dynamic libraries to generate executable files
Fourth step:./test
Running an executable file
can also directly compile the library file, omit the intermediate file
Static Library: AR-CR liba.a A.cpp
Dynamic library: g++-shared-fpic-o liba.so A.cpp