Use the GCC compiler to generate dynamic and static link libraries. The gcc compiler

Source: Internet
Author: User

Use the GCC compiler to generate dynamic and static link libraries. The gcc compiler

Reprinted please mark: http://www.cnblogs.com/winifred-tang94/

1. compilation process

Gcc-fPIC-c xxx. c

-FPIC indicates the target code that notifies the gcc compiler to generate a location independent. The link is not copied.

2. Link Process

Gcc-shared-o libxxx. so xxx. o

After compiling the link, you can generate a dynamic link library with the extension of. so.

Eg.

You can also use gcc-fPIC-shared-o libhello. so hello. c

For example:

// Hello. h

// Hello. c

// Main. c

Mr Cheng Dynamic Link Library

Then compile the link main. c.

However, if you put-lhello behind the file name, there is no problem and a main execution file is generated:

The following problems occur when running the execution file:

The reason may be that the path of the dynamic library is not known when the program runs, so it is not found.

Solution:

Copy the dynamic link library to the system shared directory, or create a connection for the dynamic link library under the system shared directory (both hard connection and symbolic connection can be used as common symbolic connections ).

Ldconfig is used to share the dynamic link library with the system.

After the dynamic link library is shared by the system, run main. The result is as follows:

Use the GCC compiler to generate a static Link Library

An example shows how to generate a static Link Library in the gcc compiler.

// Hello. h

// Hello. c

// Main. c

First, the gcc-c hello. c file generates hello. o

Then, use hello. o to generate a static Link Library.

Ar-rc-o libhello. a hello. o

Alternatively, use ar rcs-o libhello. a hello. o

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

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.