Using the GCC compiler to generate dynamic-link libraries and static-link libraries

Source: Internet
Author: User

Reprint please indicate: http://www.cnblogs.com/winifred-tang94/

1. Compilation process

Gcc–fpic–c xxx.c

Where-fpic is the target code that notifies the GCC compiler to produce a location independent. The link does not go through a copy.

2. Link process

Gcc–shared–o libxxx.so XXX.O

A compiled link allows you to generate a dynamic-link library with a. so extension.

Eg.

You can also gcc–fpic–shared-o libhello.so hello.c

As an example:

Hello.h

hello.c

Main.c

Mr. Cheng dynamic Link Library

Then compile the link main.c

But putting the-lhello behind the file name is no problem, and a main execution file is generated:

The following issues occurred while running the execution file:

The reason may be that the program runs without knowing the path to which the dynamic library is located, so it is not found naturally.

Workaround:

Copy the dynamic link library to the system share directory, or make a connection to the dynamic link library under the System share directory (both hard and symbolic connections can be used, often symbolic connections).

The Ldconfig is intended to allow the dynamic link library to be shared with the system.

After the dynamic link library is shared by the system, run main and the results are as follows:

Using the GCC compiler to generate a static link library

Use an example to illustrate how to generate a static link library in the GCC compiler

Hello.h

hello.c

Main.c

First gcc–c hello.c file generation hello.o

Then use HELLO.O to generate a static link library, using

Ar–rc–o LIBHELLO.A hello.o

Or use AR rcs–o libhello.a hello.o

Then use the static link library to compile the link main.c

Using the GCC compiler to generate dynamic-link libraries and static-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.