Create and use dynamic libraries in Linux using libtool

Source: Internet
Author: User

The fun. C, fun. H, hello. C, hello. H, and Main. c dynamic library functions are all in fun. C and hello. C.
Fun. C:

Int add (int A, int B)
{
Return A + B;
}

Fun. h:

# Ifndef _ fun_h_11
# DEFINE _ fun_h_11
Int add (int A, int B );
# Endif
----------------------------

Hello. C:

# I nclude <stdio. h>
Void output (char * ss)
{
Printf ("Hello % s/n", SS );
}

Hello. h

# Ifndef hello_h_111
# Define hello_h_111
Void output (char * ss );
# Endif
----------------------------

Main. C:

# I nclude <stdio. h>
# I nclude "Hello. H"
# I nclude "fun. H"
Void
Main ()
{
Output ("world ");
Printf ("test value: % d/N", add (1, 2 ));
}

To create and install a dynamic library using libtools:

(1)
Libtool -- mode = compile gcc-g-o-c hello. c
Libtool -- mode = compile gcc-g-o-c fun. c
Libtool -- mode = compile gcc-g-o-c main. c
# Generate their respective o files

(2)
Libtool -- mode = link GCC-g-o libhello. La hello. Lo fun. Lo-rpath/usr/local/lib-LM
# Connecting to a dynamic library file

(3)
Libtool -- mode = link GCC-g-o test main. O libhello. La-LM
# Generate the executable file test

(4)
Libtool -- mode = install CP libhello. La/usr/local/lib/libhello. La
Libtool -- mode = install-C libhello. La/usr/local/lib/libhello. La
Libtool-n -- mode = finish/usr/local/lib
Libtool install-C test/usr/local/bin/test
# Install a dynamic library

Then you can run/usr/local/bin/test. Of course, you can set the path as needed. This is a manual process and the MAKEFILE file is written as follows:

Objs = fun. O hello. o
Lo_objs = Main. Lo fun. Lo hello. Lo
Package_version =
Libdir =/usr/local/lib

ALL: Test

Install: libhello. La
 
Test: libhello. La main. o
Libtool -- mode = link GCC-g-o test main. O libhello. La-LM

Libhello. La: $ (objs)
Libtool gcc-g-o libhello. La $ (lo_objs)-rpath $ {libdir}-lm-version-Info $ {package_version}

Main. O: Main. c fun. h hello. h
Libtool -- mode = compile gcc-g-o-c main. c

Fun. O: Fun. c fun. h
Libtool -- mode = compile gcc-g-o-c fun. c

Hello. O: Hello. c hello. h
Libtool -- mode = compile gcc-g-o-c hello. c

Clean:
@ RM-f obj/* lib *. *~ * Core *. Lo *. O *. La
@ RM-RF. libs

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.