C++中的动态库和静态库

动态库和静态库

1.制作并使用静态库

静态链接:在生成可执行文件的时候将静态库的代码复制一份到可执行文件中

优点:运行时速度快

缺点:程序体积更大,并且静态库中的内容如果有更新,则需要重新编译生成程序。

代码结构

.
├── include
│   └── test.h
├── static_library
│   ├── libtest.a
│   ├── makefile
│   └── test.cpp
└── test_static_library
    ├── main
    ├── main.cpp
    └── makefile

源代码

include/test.h
class CTestLib {
public:
    void func();
};
static_library/makefile
TARGET = test
HEADPATH = ../include/
CXX = g++

all:
	$(CXX) -c $(TARGET).cpp -o $(TARGET).o -I $(HEADPATH)
	$(AR) -r lib$(TARGET).a $(TARGET).o
	rm $(TARGET).o

clean:
	rm *.a
static_library/test.cpp
#include<iostream>
#include "../include/test.h"

void CTestLib::func() {
    printf("Hello, I am func\n");
}
test_static_library/makefile
TARGET = main
#指定静态库的路径和头文件的路径
LDFLAGS = -L ../static_library/ -I ../include/
#指定在生成可执行文件时需要的静态库
LIBS = -static -ltest
CXX = g++

$(TARGET):
	$(CXX) -o $(TARGET) $(TARGET).cpp $(LIBS) $(LDFLAGS)
clean:
	rm $(TARGET)
test_static_library/main.cpp
#include<iostream>
#include "../include/test.h"

int main()
{
    CTestLib test;
    test.func();
}

制作静态库

  • 进入目录static_library:执行命令
make

生成静态库文件libtest.a,文件结构变成:

.
├── libtest.a
├── makefile
└── test.cpp

使用静态库

  • 进入目录test_static_library:执行命令
make

生成可执行文件main,文件目录变成:

.
├── main
├── main.cpp
└── makefile
  • 然后执行main
./main

结果:

Hello, I am func

2.制作并使用动态库

代码结构

.
├── dynamic_library
│   ├── libtest.so
│   ├── makefile
│   └── test.cpp
├── include
│   └── test.h
└── test_dynamic_library
    ├── main.cpp
    └── makefile

源代码

include/test.h
class CTestLib {
public:
    void func();
};
dynamic_library/makefile
TARGET = test
# 头文件路径
HEADPATH = ../include/
CXX = g++
CXXFLAGS = -c -fPIC

all:
	$(CXX) $(CXXFLAGS) $(TARGET).cpp -o $(TARGET).o -I $(HEADPATH)
	$(CXX) -shared $(TARGET).o -o lib$(TARGET).so
	rm $(TARGET).o
clean:
	rm *.so
dynamic_library/test.cpp
#include<iostream>
#include "../include/test.h"

void CTestLib::func() {
    printf("Hello, I am func\n");
}
test_dynamic_library/makefile
TARGET = main
#指定动态库路径和头文件的路径
LDFLAGS = -L ../dynamic_library/ -I ../include/
#指定在生成可执行文件时需要的动态库
LIBS = -ltest
CXX = g++

$(TARGET):
	$(CXX) -o $(TARGET) $(TARGET).cpp $(LIBS) $(LDFLAGS)
clean:
	rm $(TARGET)
test_dynamic_library/main.cpp
#include<iostream>
#include "../include/test.h"

int main()
{
    CTestLib test;
    test.func();
}

制作动态库

  • 进入目录dynamic_library执行命令:
make

生成动态库文件libtest.so,文件结构如下:

.
├── libtest.so
├── makefile
└── test.cpp

使用动态库

  • 进入test_dynamic_library目录,执行命令:
make

生成可执行文件main,文件目录变成:

.
├── main
├── main.cpp
└── makefile
  • 然后执行main
./main

结果:

Hello, I am func

执行失败显示找不到动态库的解决方案:

终端执行命令:

#必须是绝对路径
LD_LIBRARY_PATH=/home/ubuntu/Dev/MyLib/dynamic_library/
export LD_LIBRARY_PATH

然后再执行make命令。成功

可以通过

echo $LD_LIBRARY_PATH

查看LD_LIBRARY_PATH变量的值。

补充知识

动态库的搜索路径:

  • 1.编译目标代码时指定的动态库搜索路径;
  • 2.环境变量LD_LIBRARY_PATH指定的动态库搜索路径;
  • 3.配置文件/etc/ld.so.conf中指定的动态库搜索路径;
  • 4.默认的动态库搜索路径/lib
  • 5.默认的动态库搜索路径 /usr/lib
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落后的炫幕あ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值