欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

聊聊Linux下的静态库与动态库的生成与使用

程序员文章站 2022-06-09 17:29:33
...

一句话总结:静态库在链接阶段集成于程序中,程序生成可删除;动态库在程序运行时在/usr/lib/目录下查找调用,删除后错误,编译链接均需加上-fPIC

实验代码

fun.h

#ifndef _FUN_H_
#define _FUN_H_
#define ver 2.0
void print();

#endif
fun.cpp

#include <iostream>
#include "fun.h"
using namespace std;

void print()
{
	cout << "print fun" << endl;
}

main.cpp

#include <iostream>
#include "fun.h"
using namespace std;

int main()
{
	print();
	return 0;
}

1、静态库

[email protected]:~/libaray$ ls

fun.cpp  fun.h  main.cpp

[email protected]:~/libaray$ g++ -c fun.cpp 

[email protected]:~/libaray$ ls

fun.cpp  fun.h  fun.o  main.cpp

[email protected]:~/libaray$ ar -cr libfun.a fun.o

[email protected]:~/libaray$ ls

fun.cpp  fun.h  fun.o  libfun.a  main.cpp

[email protected]:~/libaray$ g++ main.cpp -lfun

/usr/bin/ld: cannot find -lfun

collect2: error: ld returned 1 exit status

[email protected]:~/libaray$ g++ main.cpp -L. -lfun

[email protected]:~/libaray$ ls

a.out  fun.cpp  fun.h  fun.o  libfun.a  main.cpp

[email protected]:~/libaray$ ./a.out 

print fun

[email protected]:~/libaray$ g++ -o main main.cpp -L. -lfun

[email protected]:~/libaray$ ls

a.out  fun.cpp  fun.h  fun.o  libfun.a  main  main.cpp

[email protected]:~/libaray$ ./main 

print fun

[email protected]:~/libaray


ar是用来创建静态库的命令

ar - create, modify, and extract from archives


2、动态库

[email protected]:~/libaray$ ls

fun.cpp  fun.h  main.cpp

[email protected]:~/libaray$ g++ -c -fPIC fun.cpp 

[email protected]:~/libaray$ ls

fun.cpp  fun.h  fun.o  main.cpp

[email protected]:~/libaray$ g++ -shared -fPIC -o libfun.so fun.o

[email protected]:~/libaray$ ls

fun.cpp  fun.h  fun.o  libfun.so  main.cpp

[email protected]:~/libaray$ g++ main.cpp -lfun

/usr/bin/ld: cannot find -lfun

collect2: error: ld returned 1 exit status

[email protected]:~/libaray$ g++ main.cpp -lfun -L.

[email protected]:~/libaray$ ls

a.out  fun.cpp  fun.h  fun.o  libfun.so  main.cpp

[email protected]:~/libaray$ ./a.out 

./a.out: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory

[email protected]:~/libaray$ g++ -o main main.cpp -L. -lfun

[email protected]:~/libaray$ ls

a.out  fun.cpp  fun.h  fun.o  libfun.so  main  main.cpp

[email protected]:~/libaray$ ./main 

./main: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory

[email protected]:~/libaray$ sudo cp libfun.so /usr/lib/

[sudo] password for zjy: 

[email protected]:~/libaray$ ./a.out 

print fun

[email protected]:~/libaray$ ./main 

print fun

[email protected]:~/libaray$ sudo rm /usr/lib/libfun.so

[email protected]:~/libaray$ ./a.out 

./a.out: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory

[email protected]:~/libaray$ ./main 

./main: error while loading shared libraries: libfun.so: cannot open shared object file: No such file or directory

[email protected]:~/libaray


-shared 该选项指定生成动态连接库(让连接器生成T类型的导出符号表,有时候也生成弱连接W类型的导出符号),不用该标志外部程序无法连接。相当于一个可执行文件

-fPIC:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的所以动态载入时是通过代码拷贝的方式来满足不同进程的需要,而不能达到真正代码段共享的目的。(实验时Ubuntu下面编译的时候不带这个选项链接时会发生错误)

[email protected]:~/libaray$ g++ -shared -fPIC -o libfun.so fun.o

[email protected]:~/libaray$ g++ -c fun.cpp 

[email protected]:~/libaray$ g++ -shared -fPIC -o libfun.so fun.o

/usr/bin/ld: fun.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC

fun.o: error adding symbols: Bad value

collect2: error: ld returned 1 exit status

[email protected]:~/libaray


-L.:表示要连接的库在当前目录中




相关标签: 静态库 动态库