g++ compiles linked C + + code, generates and uses static libraries and dynamic libraries __c++

Source: Internet
Author: User

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

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.