gpt4 book ai didi

c++ - CLion 找不到 CMake 生成的 header ?

转载 作者:搜寻专家 更新时间:2023-10-31 01:00:21 25 4
gpt4 key购买 nike

遍历 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})

模板/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"

关于c++ - CLion 找不到 CMake 生成的 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31292679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com