c++ - 带有模块化库的 C++ cmake 项目结构
问题描述
我一直在搜索,但似乎找不到关于如何使 CMAKE C++ 项目高度模块化的良好且一致的信息。
关于项目的信息:
- 创建可执行文件(不是库)的 C++ CMAKE 项目。
- 我希望项目的各个部分成为库,以便其他项目可以使用它们。
- 假设这个项目依赖于 3 个库。我们称它们为TheLib、SubLib1、SubLib2。我希望 SubLib1 和 SubLib2 是独立的,这意味着有人可以单独使用它们。但是 TheLib 依赖于两个子库。另外我应该注意,这 3 个库是由我开发的,但我想允许任何人(或未来的我)选择这 3 个库中的任何一个并将它们包含在另一个项目中。
- 我希望将这 3 个库包含在项目中,而不是期望在其他地方找到。
- 该项目还依赖于第三方库,我们称之为ExtLib。我还希望这个库包含在项目中,并且不会在其他地方找到。
- 不同的库应该有自己的 CMakeLists.txt
我想做的事:
找到一个可以传达项目架构的项目结构。
如果我不得不猜测,它会是什么样子:
project_root/
-CMakeLists.txt (the main cmake file)
-thirdparty/
---ExtLib/ (contains include/, src/, CMakeLists.txt)
-src/
---Main.cpp
---depends/
-----TheLib/
-------CMakeLists.txt (cmake file for TheLib)
-------include/
---------TheLib/
-----------TheLib.h
-------src/
---------TheLib.cpp
---------depends/
-----------SubLib1/ (contains include/, src/, CMakeLists.txt)
-----------SubLib2/ (contains include/, src/, CMakeLists.txt)
问题:(我知道他们很多,任何对他们的帮助将不胜感激)
- 您如何看待整个结构?
- 库的代码是否应该在根目录的 src 目录之外?
- 我见过一些在 src 中有 CMakeLists.txt 的项目(而不是在程序/库的根目录)。有区别吗?
- 我已经看到一些项目的库具有非常简单的 CMakeLists.txt 文件。那是应该的吗?假设 SubLib1 依赖于真正的ExtLib,它应该不包含在代码中,而是在 /opt/local/include 或类似的东西中)。SubLib1 的 CMakeLists.txt 是否应该有 find_package(ReallyExtLib) 等?(我假设没有,只是将它包含在项目的根 CMakeLists.txt 中)
- 排除第三方依赖项,您是否有项目、TheLib、SubLibX 的 CMakeLists.txt 应该是什么样子的示例?我找到了这个链接,但它似乎是关于构建一个库而不是可执行文件。
谢谢你。
解决方案
推荐阅读
- mongodb - MongoDb中JSON属性的不区分大小写查询
- macos - 如何在不上传/下载文件/安装程序的情况下触发 GateKeeper 隔离?
- postgresql - Oracle 到 EnterpriseDB 迁移 - 使用关联数组处理关联记录数组
- php - PHP CURL BadRequest
- c# - 枚举变量可以假设枚举中未定义的值吗?
- c - 如何在输出控制台中显示移动数组?
- swift - 使用 Swift 重构/组合多个几乎相同的类的推荐方法?
- python-3.x - 在 pyglet 中将非阻塞连接到阻塞事件
- database - 如何显示同一列中的表单条目(使用条件逻辑)
- ruby - Snowflake Ruby odbc 断言失败:GEOGRAPHY_type_unsupported