The generation and use of static library files and dynamic library files under Linux __linux

Source: Internet
Author: User
Existing main.cpp, Function.cpp, Function.h
Both the static library file and the dynamic library file must be generated based on the. o file.
1. Static library file (LIBXXX.A):
The prefix must be lib and the suffix must be. A.
(1) Generate:
Ar-r Libmyfunction.a FUNCTION.O
A static library file will be generated for FUNCTION.O, and the filename must be libxxx.a.
A generated static library file can be extended. When you append a new. O to the end of a static library, you need to do this again: Runlib libxxx.a
(2) Use:
Need to perform before use: Runlib libxxx.a
The source program can use functions or classes contained in the static library Libmyfunction.a, simply link the static library file at compile time:
g++-O HelloWorld main.cpp-l.-L MyFunction
Where-L is a flag that uses a static or dynamic library, followed by. Indicates that the static library is in the current directory. If the static library is under/usr/lib, it can execute: g++-o helloworld main.cpp-l/usr/lib-l function. The function after-L does not require a LIB prefix and a. A suffix, which is automatically added when the command is executed.
2. Dynamic library file (libxxx.so)
(1) Generate:
You must add the-fpic option when compiling function.cpp into FUNCTION.O to indicate that it is not memory Related:
g++-fpic-c Function.cpp
And then to the FUNCTION.O Live dynamic library file:
g++-shared-o libmyfunction.so FUNCTION.O
Where the-shared option is the flag that generates the dynamic library file, the filename must be libxxx.so.
However, the generated libmyfunction.so is not available and needs to be added to the/usr/lib directory:
sudo mv Libmyfunction.so/usr/lib
However, this is still not available, and you need to update the record file/etc/ld.so.cache that records the available dynamic library files, using the command:
Ldconfig
Updates will be made automatically. The dynamic library can be used by functions that you write later.
(2) Use:
is similar to the use of static library files:
g++-O HelloWorld main.cpp-l.-L MyFunction

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.