简体   繁体   English

如何使用 cmake 将文件夹路径包含到 C/C++ 程序中

[英]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:如果您有许多这样的定义,则更优选:

  1. Create yourproject-config.h.cmake with contents like #cmakedefine CAFFE_MODEL_PATH .使用#cmakedefine CAFFE_MODEL_PATH等内容创建yourproject-config.h.cmake
  2. Use configure_file(yourproject-config.h.cmake yourproject-config.h)使用configure_file(yourproject-config.h.cmake yourproject-config.h)
  3. Do not forget to include_directorties(${CMAKE_CURRENT_BINARY_PATH})不要忘记include_directorties(${CMAKE_CURRENT_BINARY_PATH})
  4. #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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM