Modular programming for generating function libraries and compiling methods under Linux

Source: Internet
Author: User

First, create separate source files for two functions (name FRED.C and BILL.C respectively). The following first source file:

#include "stdio.h"

void Fred (int arg)

{

printf ("Fred:we passed%d\n", ARG);

}

Here is the second source file:

#include "stdio.h"

void Bill (Char *arg)

{

printf ("Bill:we passed%s\n", ARG);

}

Second, compile these functions separately to produce the target files to be included in the library file.

$ gcc-c bill.c FRED.C

Iii. Write a program that calls the Bill function now. First create a header file for your library file. This header file will declare the functions in your library file, and he should be included with all applications that want to use your library files.

/*this is Lib.h It declares the functions Fred and bill for users * *

void Bill (char *);

void Fred (int);

The calling program (PROGRAM.C) is very simple, it contains the header file of the library and calls a function in the library.

#include "Lib.h"

int main ()

{

Bill ("Hello World");

Exit (0);

}

Five, now you can compile and test this program. You temporarily display the specified destination file for the compiler, and then ask the compiler to compile your file and link it to the previously compiled target module BILL.O.

$ gcc-c PROGRAM.C

$ gcc-o Program PROGRAM.O BILL.O

$./program

Bill:we passed Hello Word

$

Six, now, you will create and use a library file. Use the AR program to create an archive and add your target file.

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

A-bill.o

a-fred.o

Seven, the library file is created, two target files are added, in order to successfully use the function library, you also need to generate a table of contents for the function library. You can use the Ranlib command to do this work.

$ ranlib LIBFOO.A

This library can be used, and you can add the library file to the list of files used by the compiler to create your program.

$ gcc-o Program PROGRAM.O LIBFOO.A

./proram

Bill:we passed Hello World

$

You can also use the-l option to access the function library, but it does not have a standard location, so you must use the-l option to tell the compiler where to find him,

$ gcc-o Program program.o-l.-lfoo


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.