无法编译 CMake 教程示例...为什么?

use*_*882 1 c++ cmake

我正在尝试按照官方教程CMake 添加版本号和配置的头文件。我有两个目录:

  • Step1
  • Step1_build

Step1包含CMakeLists.txt,TutorialConfig.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)
Run Code Online (Sandbox Code Playgroud)

TutorialConfig.h.in

// the configured options and settings for Tutorial

#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
Run Code Online (Sandbox Code Playgroud)

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;
}
                                
Run Code Online (Sandbox Code Playgroud)

然后我尝试使用cmake -S Step1 -B Step1_build. 但我收到一个错误:

CMakeLists.txt 处的 CMake 错误:10 (target_include_directories) 无法为不是由该项目构建的目标“Tutorial”指定包含目录

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

Pta*_*666 6

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

# add the executable 
add_executable(Tutorial tutorial.cxx)
Run Code Online (Sandbox Code Playgroud)

应在调用之前移至target_include_directories.