将子目录中的源添加到cmake项目 [英] Add Source in a subdirectory to a cmake project

查看:915
本文介绍了将子目录中的源添加到cmake项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,它没有被分为图书馆,但源是组织在一个目录树。我不知道如何告诉cmake去下一个目录,然后将该目录中的源码添加到父目录中定义的项目中。我已尝试以下操作:

I have project which has not been divided into libraries, but the source is organized in a directory tree. I do not know how to tell cmake to go down a directory, then add the source in that directory to project defined in the parent directory. I have attempted the following:

in project/source/CMakelists.txt:

c $ c> set(SOURCE
$ {CMAKE_CURRENT_SOURCE_DIR} /unitTest/main.cpp

add_subdirectory($ {PROJECT_SOURCE_DIR} / folder1)
add_executable(UnitTestRNG $ { SOURCE} $ {HEADERS})

set(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/unitTest/main.cpp ) add_subdirectory("${PROJECT_SOURCE_DIR}/folder1") add_executable(UnitTestRNG ${SOURCE} ${HEADERS})

然后在project / source / folder1 / CMakeLists.txt:

then in project/source/folder1/CMakeLists.txt:

set(SOURCE
   ${SOURCE}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
)
set(HEADERS
   ${HEADERS}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
)

使用一些message()语句,我发现子文件夹将获取SOURCE变量的内容,但是对该变量的新赋值在返回父CMakeLists.txt时将不会持续。

using some message() statements, I have found that the the child folder will get the contents of the SOURCE variable, but it's new assignment to that variable will not persist on returning to the parent CMakeLists.txt

查找示例和cmake教程导致我得出结论:
- 源文件结构通常在项目内是平的
- 如果代码被分成文件夹,它通常被分为相应的库。

Looking for examples and at the cmake tutorial has led me to the conclusion that: - Source file structures are usually flat within a project - If code is divided into folders, it is usually is divided into corresponding libraries.

我想知道是否有一些最佳实践,我通过尝试这种结构偏离了它。

I wonder if there is some "best practice" from which I am deviating by attempting this structure.

推荐答案

像arrowdodger的回答的第二部分:
in project / source / folder1 / CMakeLists.txt:

Like the second part of arrowdodger's answer says: in project/source/folder1/CMakeLists.txt:

set(SOURCE
   ${SOURCE}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
   PARENT_SCOPE
)
set(HEADERS
   ${HEADERS}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
   PARENT_SCOPE
)

这篇关于将子目录中的源添加到cmake项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆