Linux Static Library Generation guide

Source: Internet
Author: User

The static library on Linux is actually the archive file of the target file.
The steps to create a static library on Linux are as follows:

    1. Write the source file by gcc -c xxx.c generating the target file.
    2. Use ar the archive target file to generate a static library.
    3. With the static library, write a header file that uses the functions in the static library.
    4. When using a static library, include the corresponding header file in the source code, and remember to link your own library when linking.

The following examples are explained in detail.

Write the source file and generate the target file.

First source file my_print.c

#include <stdio.h>void cout(const char * message){ fprintf(stdout, "%s\n", message);}

Source file 2:my_math.c

int add(int a, int b){ return a + b;}int subtract(int a, int b){ return a - b;}

Use GCC to generate the target file for these two source files:

gcc -c my_print.c my_math.c

We got MY_PRINT.O and MY_MATH.O.

Archive the target file and get the static library.

We use AR to archive the target file:

ar crv libmylib.a my_print.o my_math.o

We get the LIBMYLIB.A, which is the static library we need.

CRV is the command option for AR in the above command:

    • c If you need to generate a new library file, do not warn
    • R instead of the existing file in the library or insert a new file
    • V Output Details

ar t libmylib.aYou can view the libmylib.a target files that are included in the.

You can do this by ar --help looking for more help.

Note: The file name of the library we want to generate must look like libxxx.a , so we can use it when we link to the library -lxxx .
In turn, when we tell the compiler -lxxx , the compiler searches the specified directory libxxx.a or libxxx.so .

Generate the corresponding header file

The header file defines the LIBMYLIB.A interface, which tells the user how to use LIBMYLIB.A.

To create a new my_lib.h, write the following:

#ifndef __MY_LIB_H__#define __MY_LIB_H__int add(int a, int b);int subtract(int a, int b);void cout(const char *);#endif
Test our static Library

Under the same directory, create the TEST.C:

#include "my_lib.h"int main(int argc, char *argv[]){ int c = add(15, -21); cout("I am a func from mylib ..."); return 0;}

The and functions in LIBMYLIB.A are referenced in this source file cout add .

Compile test.c:

gcc test.c -L. -lmylib

A.out will be generated, and the program can be run through./a.out. Indicates that our static library is working properly.

The above command -L. tells GCC to include the current path when searching the link library, -lmylib telling GCC to link when generating executable programs libmylib.a .

Automation through Makefile

The above steps are cumbersome, or write a simple makefile bar, the content is as follows:

.PHONY: build testbuild: libmylib.alibmylib.a: my_math.o my_print.o    ar crv [email protected] my_math.o my_print.omy_math.o: my_math.c    gcc -c my_math.cmy_print.o: my_print.c gcc -c my_print.ctest: a.outa.out: test.c gcc test.c -L. -lmylib

After the makefile is written, the run will make build build LIBMYLIB.A and run the make test program that will generate the link libmylib.a.

If you use MinGW on Windows, it's the same way that you build a static library under Linux.

Linux Static Library Generation guide

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.