First of all, I already read How to read a CMake Variable in C++ source code, and I don't really know if it can help me.
Second, my question. Top level CMakeLists.txt says SET( MY_VAR /some/path )
and in top level source.cpp I need to achieve somehow a std::string
containing /some/path/to/files
naturally using MY_VAR
value.
You can use the configure_file(filename output_file)
command. It replaces something like ${VARIABLE}
or @VARIABLE@
in file filename
with the actual value of cmake variable VARIABLE
and writes the result to output_file
.
So, you can write something like
// in C++ code
std::string my_var = "@MY_VAR@";
# in CMakeLists.txt
configure_file(filename.h.in filename.h)
Also, I think it is a good idea for the output file to be generated somewhere inside build directory (and its' containing directory added to include paths).
configure_file in cmake documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With