0

我曾经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对那些有#defineCAFFE_MODEL_PATH, CAFFE_TRAIN_MODEL, MEAN_FILE, LABEL_FILE

但是当我在 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;
4

1 回答 1

1

configure_file, 表达式

#cmakedefine CAFFE_MODEL_PATH

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

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

如果要使用字符串值定义宏,请使用

#define CAFFE_MODEL_PATH "@CAFFE_MODEL_PATH@"

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

另请参阅命令文档configure_file

于 2017-02-13T08:14:11.010 回答