Directory:
1 manually establishing static libraries 2 use of static libraries 3 creating a static library from a makefile file
1 Creating a static library manually will create a simple static library
-1: Compile the required source files into the target file
------Helpguy.h
#ifndef __helpguy_h__#define __helpguy_h__#include <stdlib.h> #include <stdio.h> #include <unistd.h >void err_msg (const char* errmsg, ...); #endif//__helpguy_h__
------helpguy.cc
#include "helpguy.h" #include <stdarg.h> #include <errno.h> #include <string.h>void exit_msg (const char* errmsg, ...) { va_list ap; Va_start (AP, errmsg); int errno_save = errno; Char buf[1024]; vsnprintf (buf, sizeof (BUF)-1, ErrMsg, AP); if (Errno_save! = 0) { int len = strlen (BUF); snprintf (buf + len, sizeof (BUF)-Len-1, ": (%d)%s", Errno_save, Strerror (Errno_save)); } strcat (buf, "\ n"); Output fflush (stdout); Fputs (buf, stderr); Fflush (stderr); Va_end (AP); Exit (1);}
Compile:
g++-C helpguy.cc//Generate file: HELPGUY.O
2 Create the target file as a static library file the general static library name is LIBXXX.A, and when used, add-lxxx at the end of the command line, and the compilation will look for LIBXXX.A or libxxx.so files.
AR CRS libhelpguy.a HELPGUY.O//Build library file: libhelpguy.a
2 Use of static libraries
------Test File main.cc
#include "Helpguy.h" #include <iostream>int main (int argc, char** argv) { std::cout << "Please enter Positive integer: "; int value; Std::cin >> value; if (value <= 0) exit_msg ("Need positive integer"); Std::cout << "The value is:" << value << Std::endl; Std::cout << "OK" << Std::endl; return 0;}
Compile:
g++-o main main.cc-l.-lhelpguy//Generate: Main file./main When you enter a non-integer, the exit_msg is called.
-L. Tell the compiler to wash "." In the current directory. Looking for a library file named Libhelpguy.a or libhelpguy.so
3 Creating a static library from a makefile file
-----Makefile
Cflags=-g-d__stdc_format_macros-wall-werror-i.
Libs=-lrt-pthread
libhelpguy.a:helpguy.o
AR CRS [email protected] $^
chmod u+x [email protected]
helpguy.o:helpguy.cc
g++-o [email protected]-C $< $ (CFLAGS)
Clean
RM-RF HELPGUY.O
You can create a static library file by entering the make command:
makeg++-o helpguy.o-c helpguy.cc-g-d__stdc_format_macros-wall-werror-i.
AR CRS libhelpguy.a HELPGUY.O
chmod u+x libhelpguy.a
If you have more than one. o file, you can continue adding it behind LIBHELPGUY.A: and then mimic helpguy.o to add the command to generate the target file.
Linux creates static libraries and uses of static libraries