How Linux g++ uses dynamic-link libraries

Source: Internet
Author: User

Suppose the file contents are as follows:

+main.cpp//source file

+zy_ftdcmdapi.cpp//source file

+zy_ftdcmdapi.h//Header file

TRADERAPI//Sub-folders

++apiheader.h//Header file

++libthostmduserapi.so//Dynamic link library

Then the compilation process is:

g++-o m main.cpp zy_ftdcmdapi.cpp-i./traderapi/-l./traderapi-lthostmduserapi

-i./traderapi/is the path to the header file used in the source code

-L./TRADERAPI is the path to the dynamic link library that is used

-LTHOSTMDUSERAPI is a dynamic link library, note that the dynamic link library itself must be libxxxx.so this notation, compile time omit "Lib" and ". So"

Supplement 1:

When running, it is possible that the executable file m cannot find the dynamic link library and the following error occurs:

./m:error while loading shared libraries:libthostmduserapi.so:cannot open Shared object file:no such file or directory

This is because the program defaults to the/lib64/directory for the dynamic link library, and the thostmduserapi.so used in the program is not in/lib64/. Therefore, you need to add the following command to get the program to the directory of the instruction to find the library

Export Ld_library_path= $LD _library_path:./traderapi

Supplement 2:

If you do not like-lthostmduserapi this writing, or not accustomed to the dynamic Link library lib. Lib can also be removed, directly ctrip xxxx.so just compile, you need to use the dynamic link library as a. cpp file to compile.

Assuming the name of the dynamic-link library is thostmduserapi.so, then compile the command:

and this method does not occur in the "Record 1" issue . Pretty good.

How Linux g++ uses dynamic-link libraries

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.