Search code examples
c++cmakeg++cmake-gui

C++ error: expected primary-expression before ‘;’ token using Cmake


I used Cmake to define folder paths.

I have Config.in.h file and where #cmakedefine are declared as

#cmakedefine CAFFE_MODEL_PATH
#cmakedefine CAFFE_MODEL_PATH
#cmakedefine CAFFE_TRAIN_MODEL
#cmakedefine MEAN_FILE
#cmakedefine LABEL_FILE

In my CMakeLists.txt, I did as

set(CAFFE_MODEL_PATH "" CACHE PATH "Path to a Caffe model")
set(CAFFE_TRAIN_MODEL "" CACHE PATH "Path to a trained model")
set(MEAN_FILE "" CACHE PATH "Path to the mean file all trained images")
set(LABEL_FILE "" CACHE PATH "Path to the mean file all trained images")
configure_file (
  "${PROJECT_SOURCE_DIR}/Config.h.in"
  "${PROJECT_SOURCE_DIR}/Config.h"
)

So that Config.h has #define for those CAFFE_MODEL_PATH, CAFFE_TRAIN_MODEL, MEAN_FILE, LABEL_FILE.

But when I use them in my main.cpp file

int main(void) {

    ::google::InitGoogleLogging("endtoenddetection");

    string model_file   = CAFFE_MODEL_PATH;
    string trained_file = CAFFE_TRAIN_MODEL;
    string mean_file    = MEAN_FILE;
    string label_file   = LABEL_FILE;
}

I have errors as

/home/Softwares/ReInspect/endtoendLstm/main.cpp:8:43: error: expected primary-expression before ‘;’ token
     string model_file   = CAFFE_MODEL_PATH;
                                           ^
/home/Softwares/ReInspect/endtoendLstm/main.cpp:9:44: error: expected primary-expression before ‘;’ token
     string trained_file = CAFFE_TRAIN_MODEL;
                                            ^
/home/Softwares/ReInspect/endtoendLstm/main.cpp:10:36: error: expected primary-expression before ‘;’ token
     string mean_file    = MEAN_FILE;
                                    ^
/home/Softwares/ReInspect/endtoendLstm/main.cpp:11:37: error: expected primary-expression before ‘;’ token
     string label_file   = LABEL_FILE;

Solution

  • When configure_file, expression

    #cmakedefine CAFFE_MODEL_PATH
    

    is actually a conditional macro definition. That is, the macro is defined only when corresponded CMake variable is evaluated as no-false (according to if(constant) rules).

    In your case, CMake variables have empty values, which are evaluated as false. So configured file (Config.h in you case) doesn't contain macros definitions.

    If you want to define macro with string value, use

    #define CAFFE_MODEL_PATH "@CAFFE_MODEL_PATH@"
    

    Such a way the macro will be properly defined even if corresponded variable is empty.

    See also documentation for configure_file command.