[英]How to include a folder path using cmake into a C/C++ program
My c++ program needs a folder path and I like to input from cmake configuration.我的 c++ 程序需要一个文件夹路径,我喜欢从 cmake 配置输入。 For example, my c++ program is
例如,我的 C++ 程序是
int main(){
std::string pretrained_binary_proto("/home/Softwares/Libraries/caffe-master/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel");
}
I like to set this folder path using cmake.我喜欢使用 cmake 设置此文件夹路径。
/home/Softwares/Libraries/caffe-master/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
In my CMakeLists.txt
, I have在我的
CMakeLists.txt
,我有
set(CAFFE_MODEL_PATH "/home/nyan/Softwares/Libraries/caffe-master/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel")
But I don't see that CAFFE_MODEL_PATH in my ccmake.. configuration.但是我在 ccmake.. 配置中没有看到 CAFFE_MODEL_PATH 。 Then how can I include that path to my program?
那么我怎样才能将该路径包含到我的程序中呢?
The "easy" way: “简单”的方法:
add_definitions(-DCAFFE_MODEL_PATH=\"${CAFFE_MODEL_PATH}\")
and then use CAFFE_MODEL_PATH
constant in the code.然后在代码中使用
CAFFE_MODEL_PATH
常量。
More preferred way if you have many such defines:如果您有许多这样的定义,则更优选:
yourproject-config.h.cmake
with contents like #cmakedefine CAFFE_MODEL_PATH
.#cmakedefine CAFFE_MODEL_PATH
等内容创建yourproject-config.h.cmake
。configure_file(yourproject-config.h.cmake yourproject-config.h)
configure_file(yourproject-config.h.cmake yourproject-config.h)
include_directorties(${CMAKE_CURRENT_BINARY_PATH})
include_directorties(${CMAKE_CURRENT_BINARY_PATH})
#include "yourproject-config.h"
whenever and wherever you need to access your constants. #include "yourproject-config.h"
随时随地访问常量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.