cmake变量作用域,add_subdirectory [英] cmake variable scope, add_subdirectory
问题描述
我有一个CMakeLists.txt在我的项目根和一个在我的/ src文件夹。 / src文件夹中的一个只包含一个带有.cpp文件的变量( set(SOURCEFILES main.cpp foo.cpp)
),在根CMakeLists.txt add_subdirectory(src)
以后我会 add_executable(MyApp $ {SOURCEFILES})
。
I have a CMakeLists.txt in my project root and one in my /src folder. The one in the /src folder only contains a variable with the .cpp files (set (SOURCEFILES main.cpp foo.cpp)
) and in the root CMakeLists.txt I do add_subdirectory(src)
and later I do add_executable(MyApp ${SOURCEFILES})
.
但是cmake给我的错误
But cmake gives me the error
add_executable调用的参数数量不正确,没有来源
提供
add_executable called with incorrect number of arguments, no sources provided
如何获取cmake来查看变量?我读到,cmake只知道全局变量,但显然不是这样...
How do I get cmake to see the variable? I read that cmake only knows global variables, but that's obviously not the case...
推荐答案
set 命令,每个目录都添加了 add_subdirectory
或用 function
声明的每个函数创建一个新作用域。
As mentioned in the documentation of the set command, each directory added with add_subdirectory
or each function declared with function
creates a new scope.
从其父作用域继承所有变量定义。使用 set
命令的新子系统中的变量赋值只会在子作用域中可见,除非 PARENT_SCOPE
用过的。
The new child scope inherits all variable definitions from its parent scope. Variable assignments in the new child sope with the set
command will only be visible in the child scope unless the PARENT_SCOPE
option is used.
要使 SOURCEFILES
作业在项目的根文件夹中可见,请尝试:
To make the SOURCEFILES
assignment visible in the root folder of your project, try:
set (SOURCEFILES main.cpp foo.cpp PARENT_SCOPE)
这篇关于cmake变量作用域,add_subdirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!