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

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

我用Cmake定义了folder paths

我有 Config.in.h 文件,其中 #cmakedefine 声明为

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

在我的 CMakeLists.txt 中,我做了

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"
)

因此 Config.h 为那些 CAFFE_MODEL_PATH, CAFFE_TRAIN_MODEL, MEAN_FILE, LABEL_FILE 设置了#define。

但是当我在 main.cpp 文件中使用它们时

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;
}

我有错误

/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;

configure_file,表达式

#cmakedefine CAFFE_MODEL_PATH

实际上是一个条件宏定义。也就是说,仅当相应的 CMake 变量被评估为 no-false(根据 if(constant) 规则)时才定义宏。

在您的情况下,CMake 变量具有 值,这些值被评估为 false。这样配置的文件(Config.h 在你的情况下)不包含宏定义

如果你想用字符串值定义宏,使用

#define CAFFE_MODEL_PATH "@CAFFE_MODEL_PATH@"

这样即使对应的变量为空,宏也能正确定义。

另请参阅 documentation 以获取 configure_file 命令。