如何在不同的CMake文件之间共享变量 [英] How to share variables between different CMake files
问题描述
如何在不同的CMake文件之间共享变量,并显示以下示例来说明我的问题:
How do I share variables between different CMake files, and I show the following examples to illustrate my question:
cmake_minimum_required(VERSION 2.6)
project(test)
set(Var3 "Global variable")
add_subdirectory(${test_SOURCE_DIR}/exe)
add_subdirectory(${test_SOURCE_DIR}/dll)
EXE文件
EXE file
set(Var1 "this is variable 1")
set(Var1 ${Var1} " added to varible 1")
message(STATUS ${Var1})
DLL文件
DLL file
set(Var2 "this is variable 2")
message(STATUS ${Var2})
message(STATUS ${Var1})
message(STATUS ${Var3})
在此示例中,Var3可以可以在 exe
和 dll
的CMake文件中看到,该文件在 Main 中定义code>。但是,在
exe
中定义的Var1在 dll
中不会被观察到。我只是好奇:有没有办法使 exe
中定义的Var1在 dll
中可见?
In this example, Var3 can be seen in the CMake files of exe
and dll
as it is defined in Main
. However, Var1, which is defined in exe
, will not be observed in dll
. I was just curious: is there a way to make Var1 defined in exe
observable in dll
?
推荐答案
除了Tadeusz正确说的以外,还可以通过使用
Beside what Tadeusz correctly said, you can make a variable visible at any level (not just one up!) by using
set(Var1 "This is variable 1" CACHE INTERNAL "")
该变量将可用于该指令之后的所有CMake指令,因此,例如,对于在定义此变量的目录之前添加的姊妹目录,该变量将不可用
The variable will be available for all the CMake instructions that follow that instruction, so for example it won't be available for a sister directory that is added before the directory where this variable is defined.
这篇关于如何在不同的CMake文件之间共享变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!