The static library and dynamic library of C language shared code

Source: Internet
Author: User

Sharing code can improve efficiency, but for code security, for reasons like this, it is generally rare to keep C files, instead of compiling C files into. o files,

Shared code is implemented by sharing h files and. o files.


Multiple. o files are cumbersome to manage, and if you package these files into a library, it is a common method to call the functions inside through the link library.

It is generally divided into static libraries (archives) and dynamic libraries.



Create an archive

The archive is a static library in the form of libxxx.a. You can use the AR command to create an archive
Ar-rcs libxxx.a a.o b.o ...
At this point, A.O and B.O are packaged in a static library of LIBXXX.A


Use:

<1> put LIBXXX.A in a standard directory
At this point, you can link the library without specifying the path to the library file gcc-lxxx

<2> put libxxx.a in a directory of your own set
At this point, you need to specify the library directory Gcc-l/path/-lxxx





Create a dynamic library


gcc-shared x.o-o libxxx.so (Linux or Unix) Libxxx.dylib (MAC)


Use the same method as the static library.













Static libraries and dynamic libraries for C-language shared code

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.