cmake - 从更高级别的目录链接自定义库
我正在学习 cmake 并且发现它学起来非常快。然而,文档还不够完善。这些教程很有帮助,但也仅限于此。
我拥有的是一个现有项目,我想使用 cmake 转换为该项目。
目前的项目结构如下:
obsys/ obsys/client/ {客户端文件} obsys/server/ {服务器文件} obsys/utils/包括 obsys/utils/src
我在每个级别创建了 CMakeLists.txt 文件。
目前顶层看起来像这样:
make_minimum_required (VERSION 2.6)
project (osys)
add_subdirectory (server)
add_subdirectory (utils)
在 utils 内部,我有一个 CMakeLists.txt,其中包含:
include_directories ("${PROJECT_SOURCE_DIR}/utils/include")
add_subdirectory (src)
在 utils src 目录中,CMakeLists.txt 文件包含:
add_library(utils myException.cpp)
服务器需要链接到 utils。它的 CMakeLists.txt 包含:
make_minimum_required (VERSION 2.6)
project (server)
include_directories ("../utils/include")
add_executable(serv serv.cpp)
add_subdirectory("../utils/src" utils)
target_link_libraries(obbsd utils)
这种布局的想法是我希望我可以通过顶级 Makefile 构建全部内容。或者,跳入服务器或客户端并构建其中一个。事情并非如此。推荐的方法是什么?
I'm learning about cmake and am finding it very fast to learn. However, documentation is not up to scratch. The tutorials are helpful, but only go so far.
What I have is an existing project that I want to convert to using cmake.
The project structure currently looks like:
obsys/
obsys/client/ {client files}
obsys/server/ {server files}
obsys/utils/include
obsys/utils/src
I've created CMakeLists.txt files at each level.
The top level looks currently like this:
make_minimum_required (VERSION 2.6)
project (osys)
add_subdirectory (server)
add_subdirectory (utils)
Inside utils I have a CMakeLists.txt that contains:
include_directories ("${PROJECT_SOURCE_DIR}/utils/include")
add_subdirectory (src)
and inside the utils src directory the CMakeLists.txt file contains:
add_library(utils myException.cpp)
The server needs to link to utils. It's CMakeLists.txt contains:
make_minimum_required (VERSION 2.6)
project (server)
include_directories ("../utils/include")
add_executable(serv serv.cpp)
add_subdirectory("../utils/src" utils)
target_link_libraries(obbsd utils)
The idea with this layout is I was hoping I could build the whole lot through the top level Makefile. Or, jump into server, or client and build either of them. It's not working out that way. What is the recommended way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我现在可以使用了。
问题是我的 add_executable 行没有列出所有源文件。
Link 似乎在抱怨与 utils 有关的事情,这让我认为它实际上没有找到库。
Ok, I now have it working.
The problem is that my add_executable line wasn't listing all the source files.
Link seemed to be complaining about something to do with utils which made me think it wasn't finding the library when actually it was.