Linux static library generation and use)

Source: Internet
Author: User

A library is a software component technology that encapsulates data and functions.

The use of libraries can modularize the program. Windows systems include static link libraries (. lib files) and dynamic link libraries (. dll files ). Linux stores the library files in the/usr/lib or/lib directory. The Linux library file name consists of three parts: prefix lib, library name, and suffix. The dynamic link library uses. so as the most suffix, and the static link library usually uses. a as the suffix. When static and dynamic libraries are used in programs, Their Loading Order is different. Static library code is copied to the application during compilation. This advantage is that it saves Compilation Time. The dynamic link library is loaded only when the program calls the library function after it starts running.
Create and use a static library: 1. Declare the function exported by the static library in a header file. 2. Implement the function exported from the static library in a source file. 3. Compile the source file and generate executable code. 4. Add the target file of the executable code to a static library, and copy the static library to the default directory where the library file is stored. The following example shows that mylib. h stores the declaration of functions provided by the static library for users. mylib. c implements the declaration of mylib. h.
Header file: mylib. h
#ifndef _MYLIB_H_#define _MYLIB_H_ void weclome(void);void outString(const char *str); #endif

Source File: mylib. c

#include "mylib.h"#include  void welcome(void){    printf("welcome to libmylib\n");} void outString(const char *str){    if(str != NULL)        printf("%s\n", str);}

1> compile mylib. c to generate the target file: gcc-o mylib. o-c mylib. c

2> Add the target file to the static library: ar rcs libmylib. a mylib. o3> copy the static library to the Linux library directory (/usr/lib or/lib): cp libmylib. a/usr/lib/libmylib. a. Compile the test program that calls the library function. c:
#include "mylib.h"#include  int main(void){    printf("create and use library:\n");    welcome();    outString("it's successful\n");     return 0;}

4> compile with a static library: gcc-o test. c-lmylib

Note that the prefix and suffix are not required during compilation. 5> run the executable program test:./test
create and use library:welcome to libmylibit's successful

In Linxu, you can use the ar command to create and modify static databases.

Here, we can get the parameters in man ar in linux. Several common d: delete member files from the library. R: Add a member file to the database. If it exists, replace it. C: Create a database. S: The Library symbol table is forcibly regenerated regardless of whether the ar command modifies the library content. Use man again for other commands.
The above five steps are basically used. If you still need them, you need to study them in depth.

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.