"Summary" Linux under the development of C, how to make a library file, and how to reference this library file?

Source: Internet
Author: User

This article according to Http://topic.csdn.net/u/20091026/10/0e913e37-e4a2-4504-bcc7-61af18a22dde.html?6487 's reply summarizes.

1. Shared Library

Often made into a shared library, when the-shared parameters, and the link generated file name libxxx.so, can be placed under/usr/lib

When other programs refer to the library, add-lxxx to the link.

After you edit the code, use a command similar to the following at compile time to generate a shared library (if the source file is MYFUN.C):
gcc-shared-fpic-wmyfun.c-0 libmy.so
Explanation:-shared generates shared libraries;
-FIPC denotes "position-independent code" (Position independent), which is not added;
-W indicates an output warning message, which is not added;
The name of the library file should be in libxxxx.so format;

To reference this library:
GCC xxx.c-0 xxx.out-lmy
-L indicates that the library to be connected is set (libmy.so, and I can be lost)

2. Static Library

FileName: LIB.C
First compile into target file Gcc-o mylib.o-c LIB.C
The static library is then created to place the MYLIB.O in the LIBMYLIB.A ar RCS LIBMYLIB.A MYLIB.O
Finally copy to the library directory/usr/lib CP LIBMYLIB.A/USR/LIB/LIBMYLIB.A

The function that is called directly at the time of writing is, just add the-l option after compiling to follow the library name
such as Gcc-o test Test.c-lmylib//mylib is the name of the library created earlier

3. Dynamic Library

Creation is Gcc-fpic-share-o mylib.so LIB.C
Calls are also divided into two ways
One way is to Gcc-o test test.c./libmylib.so (Note that this is the path to the dynamic library)
The second method is to use the functions of a dynamic library. You man dlopen look at those functions and you'll know it's not much to say.

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.