C++ 如何将结构引入现代CMake包含路径
我试图使用现代的cmake,但我遇到了这样一个问题:我所有的C++ 如何将结构引入现代CMake包含路径,c++,cmake,C++,Cmake,我试图使用现代的cmake,但我遇到了这样一个问题:我所有的#include语句中都没有文件夹,这让人非常困惑 项目结构: root: - CMakeLists.txt - core - CMakeLists.txt - core.h - core.cpp - bin - bin.cpp - CMakeLists.txt - other_dirs - ... 根目录CMakeLists.txt cmake_minimum_requir
#include
语句中都没有文件夹,这让人非常困惑
项目结构:
root:
- CMakeLists.txt
- core
- CMakeLists.txt
- core.h
- core.cpp
- bin
- bin.cpp
- CMakeLists.txt
- other_dirs
- ...
根目录CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(scratch)
add_subdirectory(core)
add_subdirectory(bin)
add_executable(x2_cli x2_cli.cpp)
target_link_libraries(x2_cli core)
bin/CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(scratch)
add_subdirectory(core)
add_subdirectory(bin)
add_executable(x2_cli x2_cli.cpp)
target_link_libraries(x2_cli core)
core/CMakeLists.txt
add_library(core core.cpp core.h)
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
我想写我的include语句,比如#include“core/core.h”
,但是有了这个策略,我必须只写#include“core.h”
。我认为这是非常混乱的,因为您不知道什么头文件属于什么库
有没有一种方法可以解决这个问题,它不允许我从其他文件夹中包含我不应该包含的东西?例如#include other_dir/bad.h
理想情况下应该在编译时失败。我需要不同的目录结构吗?也许有一个很好的方法
仅仅编写
target\u include\u目录(核心公共..)
是不够好的,因为它污染了我的include路径,并且具有误导性。您的项目结构中可能需要一些子文件夹(参见示例)。我经常看到,如果您使用类似于#include“core/core.h”
的内容,您的文件夹结构中会有反映这一点的内容,例如,通过include/core/core.h
。另一个示例: