[英]Conditionally add files to library in C++ project
I have a CMake C++ project that consist of a library and multiple executable applications. 我有一个CMake C ++项目,它包含一个库和多个可执行应用程序。 The library contains many files and conditionally depending on whether the user wants to have GPU acceleration the list of files in the library should be different eg
该库包含许多文件,并且有条件地取决于用户是否想要GPU加速,库中的文件列表应该是不同的,例如
if (GPU_ACCELERATED)
add_library(my_library
file1.h
file1.cc
gpu_file2.h
gpu_file2.cc
)
else()
add_library(my_library
file1.h
file1.cc
)
endif()
This is one way of doing it but the problem is that I have a massive number of files and not just file1.h
file1.cc
therefore I'd very strongly prefer to avoid duplicating the file listing like that. 这是一种方法,但问题是我有大量的文件,而不仅仅是
file1.h
file1.cc
因此我非常希望避免像这样重复文件列表。 I would rather like something like this to work (but doesn't) eg 我宁愿喜欢这样的东西(但不是),例如
add_library(my_library
file1.h
file1.cc
if (GPU_ACCELERATED)
gpu_file2.h
gpu_file2.cc
endif()
)
Use the set
command to create and update CMake variables: 使用
set
命令创建和更新CMake变量:
set(my_SOURCES file1.h file1.cc)
if (GPU_ACCELERATED)
set(my_SOURCES ${my_SOURCES} gpu_file2.h gpu_file2.cc)
endif()
add_library(my_library ${my_SOURCES})
This will define a my_SOURCES
variable, if GPU_ACCELERATED
is true it will update it. 这将定义一个
my_SOURCES
变量,如果GPU_ACCELERATED
为true,它将更新它。 Then create your library from the content of the variable. 然后根据变量的内容创建库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.