【CMake系列】05-静态库与动态库编译

在各种项目类型中,可能我们的项目就是一个 库 项目,向其他人提供 我们开发好的 库 (windows下的 dll /lib ; linux下的 .a / .so);有时候在一个项目中,我们对部分功能 打包成 库,方便在不同地方进行调用

静态库 和 动态库 对项目 实现了 不同程度的解耦,

  • 静态库,往往会 链接时 加载,代码会复制一起打包到 可执行程序中
  • 动态库 更多是 运行时 加载,加载到内存,供可执行程序调用

学会使用 cmake 构建 静态库 和 动态库,是我们这篇文章的主要内容

本专栏的实践代码全部放在 github 上,欢迎 star !!!

如有问题,欢迎留言、或加群【392784757】交流

涉及命令

add_library(<name> [<type>] [EXCLUDE_FROM_ALL] <sources>...)

其中 type 用来指定构建库的类型【本篇 只针对 shared static 展开】
type

  • shared
  • static
  • ...

使用库

# 路径配置相关 有时候需要
include_directories()
link_directories()

target_link_libraries() # 关键代码 

代码部分

log/
|---|---|log.cpp
|---|---|log.h
|---|---|CMakeLists.txt
test_log/
|---|---|test_log.cpp
|---|---|CMakeLists.txt
CMakeLists.txt

log.h

// log.h
#ifndef LOG_H
#define LOG_H


class Log
{
public:
  Log();
};
#endif

log.cpp

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

Log::Log()
{
  cout << "create Log" << endl;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)

project(log)

add_library(log STATIC log.cpp log.h)

test_log 下

test_log.cpp

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

int main()
{
  Log log;
  cout << "test log" << endl;
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.20)

project(test_log)

include_directories("../log") # 头文件路径

# 指定库查找路径 windows 自动找 ../log/build/Debug ../log/build/Release
link_directories("../log/build")

add_executable(test_log test_log.cpp)

#指定加载的库
target_link_libraries(test_log log)

构建静态库

type 指定的是 static

cd log
cmake -B build -S .
cmake --build build

cd test_log
cmake -B build -S .
cmake --build build

具体的还可以指定 lib的输出路径

动态库构建

我们 在 log 同级目录下 创建 CMakeLists.txt 内容如下

cmake_minimum_required(VERSION 3.20)

project(log)

include_directories(log)

# 添加log库编译 项目自带预处理变量 log_EXPORTS
add_library(log SHARED log/log.cpp)

add_executable(test_log test_log/test_log.cpp log)


target_link_libraries(test_log log)

windows下构建动态库存在问题

如果我们把 STATIC 改为 SHARED ,然后构建,发现会报错

LINK : fatal error LNK1104: 无法打开文件“Debug\log.lib”

其实 也就是 找不到.lib 文件

log.h 修改

// log.h
#ifndef LOG_H
#define LOG_H

// __declspec(dllexport) 导出 log 到lib文件中

#ifndef _WIN32
#define CPP_API
#else
// 针对 windows 平台
// log 库文件调用 dllexport
// test_log 调用 dllimport
// 通过预定义宏 确定是谁在调用
#ifdef log_EXPORTS
#define CPP_API __declspec(dllexport) // 库项目调用
#else
#define CPP_API __declspec(dllimport) // 调用库项目 调用
#endif
#endif

class CPP_API Log
{
public:
  Log();
};
#endif

需要将 dll 中定义的 函数、类符号 等导出到lib 才能找到 【也就是 符号导出】

  • dll 二进制代码
  • lib 函数 符号 地址 【理解程度有限】

做法就是在 函数、类等 前面 添加 _declspec(dllexport),

关于 dllexport dllimport 的更详细解释,推荐这个
https://zhuanlan.zhihu.com/p/680966160

然后进行构建,成功!
在这里插入图片描述
在这里插入图片描述

posted @   蔡不菜和他的uU们  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示