CLion 找不到生成的 CMake headers?

CLion can't find CMake generated headers?

正在经历 CMake tutorial:

├── CMakeLists.txt
├── src
│   └── main.cpp
└── templates
    └── fooConf.h.in

CMakeLists.txt

cmake_minimum_required(VERSION 3.2)
project(foo)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(PROJECT_SOURCE_DIR src)
set(PROJECT_TEMPLATE_DIR templates)

set(SOURCE_FILES ${PROJECT_SOURCE_DIR}/main.cpp)
set(${PROJECT_NAME}_MAJOR 0)
set(${PROJECT_NAME}_MINOR 1)
set(${PROJECT_NAME}_MICRO 1)
configure_file (
  "${PROJECT_TEMPLATE_DIR}/fooConf.h.in"
  "${PROJECT_SOURCE_DIR}/fooConf.h"
)

add_executable(foo ${SOURCE_FILES})

templates/fooConf.h.in

#define @PROJECT_NAME@_VERSION_MAJOR @PROJECT_NAME@@_VERSION_MAJOR@
#define @PROJECT_NAME@_VERSION_MINOR @PROJECT_NAME@@_VERSION_MINOR@
#define @PROJECT_NAME@_VERSION_MICRO @PROJECT_NAME@@_VERSION_MICRO@

src/main.cpp

#include <iostream>

#include "src/fooConf.h"
// Also tried: "fooConf.h"

int main() {
    std::cout << foo_VERSION_MAJOR;
    return 0;
}

[错误]

fatal error: src/fooConf.h: No such file or directory

你的 fooConf.h 头文件是在 binary 树中生成的(准确地说,在 ${CMAKE_BINARY_DIR}/src/ 下)。因此,您应该发出相应的 include_directories() 命令以在 #include 中使用该文件。例如:

CMakeLists.txt

....
include_directories(${CMAKE_BINARY_DIR})
add_executable(foo ${SOURCE_FILES})

src/main.cpp

....
#include "src/fooConf.h"