Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a CMake variable to C++ source code [duplicate]

Tags:

c++

cmake

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.

like image 570
Javier Avatar asked Mar 07 '14 19:03

Javier


1 Answers

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

like image 76
lisyarus Avatar answered Sep 21 '22 15:09

lisyarus