无法获取要编译的 CMake 教程示例...为什么?

Unable to get CMake Tutorial example to compile... Why?

我正在尝试按照 CMake for adding a version number and configured header file 的官方教程进行操作。我有两个目录:

Step1 包含 CMakeLists.txtTutorialConfig.h.intutorial.cxx。每个文件的内容如下:

CMakeLists.txt

cmake_minimum_required(VERSION 3.10)

# set the project name
project(Tutorial VERSION 1.0)

# configure a header file to pass the version number to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)

# add directory to list of paths to search for include files
target_include_directories(Tutorial PUBLIC 
                           "${PROJECT_BINARY_DIR}"
                          )

# add the executable 
add_executable(Tutorial tutorial.cxx)

TutorialConfig.h.in

// the configured options and settings for Tutorial

#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

tutorial.cxx

#include "TutorialConfig.h"
#include <iostream>

int main(int argc, char *argv[]) {
    if (argc < 2) {
        std::cout << argv[0] << "Version" << Tutorial_VERSION_MAJOR << "."
            << Tutorial_VERSION_MINOR << std::endl;
        return 1;
    }
    std::cout << "argc=" << argc << std::endl;
    std::cout << "argv[1]=" << argv[1] << std::endl;
}
                                

然后我尝试使用 cmake -S Step1 -B Step1_build 进行构建。但是我得到一个错误:

CMake Error at CMakeLists.txt:10 (target_include_directories) Cannot specify include directories for target "Tutorial" which is not built by this project

我一遍又一遍地检查说明,我不确定我做错了什么。为什么项目无法编译?

在 CMake 中,需要在对其属性进行任何修改(如包含目录)之前定义目标,因此这一行:

# add the executable 
add_executable(Tutorial tutorial.cxx)

应在调用 target_include_directories 之前移动。