简体   繁体   中英

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. For example, my c++ program is

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.

/home/Softwares/Libraries/caffe-master/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel

In my CMakeLists.txt , I have

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. 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.


More preferred way if you have many such defines:

  1. Create yourproject-config.h.cmake with contents like #cmakedefine CAFFE_MODEL_PATH .
  2. Use configure_file(yourproject-config.h.cmake yourproject-config.h)
  3. Do not forget to include_directorties(${CMAKE_CURRENT_BINARY_PATH})
  4. #include "yourproject-config.h" whenever and wherever you need to access your constants.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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