C/C++ 静态库动态库实践
作者:互联网
Source File
List:
- mylib.c
- mylib.h
- test.c
mkdir library
andcd library
mkdir lib include
vim include/mylib.h
#ifndef __MYLIB_H__
#define __MYLIB_H__
int qpow(int, int);
#endif /* __MYLIB_H__ */
vim mylib.c
/******************************************************************************
* File: mylib.c
*
* Author: Gsharp
* Created: 05/15/21
* Description: library mylib implements
*****************************************************************************/
#include "mylib.h"
int qpow(int a, int n) {
int res = 1;
if (a < 0 || n < 0) {
return -1;
}
while (n) {
if (n & 1) res *= a;
a *= a;
n >>= 1;
}
return res;
}
- wirte a test
#include <stdio.h>
#include "mylib.h"
int main(int argc, char *argv[]) {
printf("%d ^ %d = %d", 3, 10, qpow(3, 10));
return 0;
}
Static Library
Build
gcc -g -Wall -Werror -fPIC -c lib/mylib.c -I./include
you will get mylib.oar rcs lib/libmylib.a mylib.o
Usage
gcc test.c -static -I include/ -L lib/ -lmylib -o test
Dynamic Library
Build
gcc -g -Wall -Werror -Wextra -fPIC -shared -pedantic mylib.c -o lib/libmylib.so
Usage
export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
gcc test.c -I inlude/ -L lib/ -lmylib -o test
标签:__,include,lib,mylib,静态,C++,int,test,动态 来源: https://www.cnblogs.com/sonnet/p/15187479.html