I want to set a compilation option -DVAR=xxx
. I know that I can do this with target_compile_options
but my issue is that the value xxx
has to be dynamically generated and is only available as the content of a txt file.
Here is my attempt:
- create a custom command that generates the file holding the value to be used
- create a custom target that allows to build the
var.txt
file - create a dependency in order to be sure that the
var.txt
will be generated before the compilation ofMyExe
but I don't kwow how to retrieve the required value from the file and use it in target_compile_options
.
cmake_minimum_required(VERSION 3.13)project ("MyProject")add_custom_command ( OUTPUT var.txt COMMAND echo "123" > var.txt # just an example here, the real generation is more complex COMMENT "Generate variable value")add_executable (MyExe main.cpp)target_compile_options (MyExe PUBLIC -DVAR=???) # how to get the content of "var.txt" for setting VAR ?add_custom_target (var ALL DEPENDS var.txt)add_dependencies (MyExe var)
Is it possible for target_compile_options
to use values retrieved from a generated file ?