Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将cmake变量转换为C++;密码?_C++_Makefile_Cmake_Cmakelists Options - Fatal编程技术网

C++ 如何将cmake变量转换为C++;密码?

C++ 如何将cmake变量转换为C++;密码?,c++,makefile,cmake,cmakelists-options,C++,Makefile,Cmake,Cmakelists Options,例如,在CMakeLists.txt中,我可以定义路径: add_definitions(-DMY_PATH="/my/path") 在C++代码中,我可以通过< /P>访问变量 std::string my_path = MY_PATH 这个很好用。现在,如果我改为在CMakeLists.txt中定义一个变量,如下所示: add_definitions(-DMY_PATH=${CMAKE_BINARY_DIR}) 然后,我得到以下错误: error: expected

例如,在CMakeLists.txt中,我可以定义路径:

add_definitions(-DMY_PATH="/my/path")
<>在C++代码中,我可以通过< /P>访问变量
std::string my_path = MY_PATH
这个很好用。现在,如果我改为在CMakeLists.txt中定义一个变量,如下所示:

add_definitions(-DMY_PATH=${CMAKE_BINARY_DIR})
然后,我得到以下错误:

error: expected primary-expression before ‘/’ token
/home/user/development/include/helper.hpp:33:32: note: in expansion of macro ‘MY_PATH’
       const std::string path = MY_PATH;
                                ^~~~~~~
<command-line>:0:10: error: ‘home’ was not declared in this scope
/home/user/development/include/helper.hpp:33:32: note: in expansion of macro ‘MY_PATH’
       const std::string path = MY_PATH;
错误:应在“/”标记之前使用主表达式
/home/user/development/include/helper.hpp:33:32:注意:在宏“MY_PATH”的扩展中
const std::string path=MY_path;
^~~~~~~
:0:10:错误:未在此作用域中声明“home”
/home/user/development/include/helper.hpp:33:32:注意:在宏“MY_PATH”的扩展中
const std::string path=MY_path;

那么,这有什么不同呢?在使用这个定义时,如何将CMAGE变量转换成C++代码中的字符串?

 add_definitions(-DMY_PATH=${CMAKE_BINARY_DIR})
 add_definitions(-DMY_PATH="${CMAKE_BINARY_DIR}")
您将在代码中获得如下内容:

 std::string my_path = /home/my_path
这显然会导致语法错误,您需要添加双引号: