简体   繁体   中英

CLion can't find CMake generated headers?

Going through the 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;
}

[Error]

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

Your fooConf.h header file is generated in the binary tree (precisely, under ${CMAKE_BINARY_DIR}/src/ ). So you should issue corresponded include_directories() command for use that file in #include . Eg:

CMakeLists.txt

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

src/main.cpp

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM