3

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?

batuman
  • 7,066
  • 26
  • 107
  • 229

1 Answers1

3

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_directories(${CMAKE_CURRENT_BINARY_DIR})
  4. #include "yourproject-config.h" whenever and wherever you need to access your constants.
Dawid
  • 477
  • 3
  • 14
arrowd
  • 33,231
  • 8
  • 79
  • 110
  • If I follow the preferred way, where to set the CAFFE_MODEL_PATH. If I do in CMakeLists.txt like option (CAFFE_MODEL_PATH "set path" "/home/nyan/Softwares/Libraries/caffe-master/models/bvlc_reference_caffenet/deploy.prototxt") I can change ON/OFF only at ccmake. How can I set the the whole path at ccmake? – batuman Feb 13 '17 at 06:01
  • Don't declare it as option but `set(CAFFE_MODEL_PATH "" CACHE PATH "Path to a Caffe model")`. `option` is just a shortcut for `set` with `BOOL` type. – arrowd Feb 13 '17 at 06:05
  • 1
    Step 3: `include_directories(${CMAKE_CURRENT_BINARY_DIR})` – Dawid Dec 17 '22 at 16:47