Use a user-defined library function in linux [linuxc]

Source: Internet
Author: User
In linux, use the User-Defined library function [linuxc] -- Linux general technology-Linux programming and kernel information. The following is a detailed description. In linux C language programming, we often use the static library defined by the system (. a) and dynamic library (. so). For example, if we want to use the mathematical function sin, we need to use-lm to name the library to be used during connection, such

$ Gcc-o fact. c-lm

Here, we use-lm to indicate the library function/usr/lib/libm. a to be linked to the static library. The system library is generally in the/lib,/usr/lib directory. If the system library function is used, you do not need to specify the path, but if it is our own library function, you need to use the-L parameter to name the path.

Here is an example:
/* Fred. c */
# Include

Void fred (int arg)
{
Printf ("fred: you passed % d \ n", arg );
}

/* Bill. c */
Void bill (char * arg)
{
Printf ("bill: you passed % s \ n", arg );
}

We write a function that promises the string and integer numbers in two files respectively, and then write a header file for them to facilitate our use:
/* Lib. h */
Void fred (int );
Void bill (char *);

Use gcc to compile the two files separately:

$ Gcc-c fred. c bill. c

-The c parameter indicates compilation (compile). Only the corresponding. o file is generated.

The following two functions are applied:
/* Program. c */
# Include "lib. h"

Int main ()
{
Fred (7 );
Bill ("Hello, linux c .");
Exit (0 );
}

Okay. Now let's compile and connect it:
$ Gcc-c program. c
$ Gcc-o program. o fred. o bill. o
$./Program

You will see the output:
Fred: you passed 7
Bill: you passed Hello, linux c.

Of course, here we compile it directly without using a library. We can use the ar command to make fred. o and bill. o a. a static library.

$ Ar crv libfoo. a bill. o fred. o

To be compatible with linux of different versions, we also need to create an entry table for it. Of course, this is not necessary, because many linux do not need this step, however, the addition is harmless:

$ Ranlib libfoo.

Now we can use our own static Link Library:
$ Gcc-o program. o-L.-lfoo
$./Program

Here,-L. indicates that the library function under the local directory (.) is used during the link.-lfoo indicates that the library function is libfoo. a or libfoo. so.

Note: The above example is from the third edition of linux program design. If you want to learn more, refer to this book.

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.