Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 借助目标目录减少标题的长度_C++_Cmake - Fatal编程技术网

C++ 借助目标目录减少标题的长度

C++ 借助目标目录减少标题的长度,c++,cmake,C++,Cmake,我有一个项目 ├── CMakeLists.txt │   ├── log │   │   ├── CMakeLists.txt │   │   ├── include │   │   │   ├── log.h │   │   └── src │   │   ├── log.cpp │   └── main.cpp 在log.cpp中我使用#include“./include/log.h”并在main.cpp中使用#include“include/log.h” 我想使用#包括“log

我有一个项目

├── CMakeLists.txt
│   ├── log
│   │   ├── CMakeLists.txt
│   │   ├── include
│   │   │   ├── log.h
│   │   └── src
│   │       ├── log.cpp
│   └── main.cpp
在log.cpp中我使用
#include“./include/log.h”
并在main.cpp中使用
#include“include/log.h”

我想使用
#包括“log.h”

我读到target\u include\u目录可以帮助我

如何将其应用于我的CMakeLists.txt

cmake_minimum_required(VERSION 3.18)

project(Logger)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#include_directories(log/include/) -- I used this, but I want to use target_include_directories

add_library(log_lib log/src/log.cpp)

add_executable(demo main.cpp)
target_link_libraries(demo log_lib)

定义目标,然后使用名称作为
target\u include\u目录的第一个参数

add_library(log_lib log/src/log.cpp)
target_include_directories(log_lib PUBLIC log/include)
在你把所有的工作都做好并且想要更好地理解它之后,担心
INTERFACE
vs
PUBLIC
vs
PRIVATE
。(此选项会间接影响依赖于您的库的目标)。

下面的行有帮助

target_include_directories(log_lib PUBLIC log/include)

总之,不要把路径放在
#include
语句中。根据我的经验,头文件可以移动。项目可以驻留在不同的沙盒中。例如,我使用“\sandbox”,而我的队友使用“\asandbox”。让build实用程序为头文件指定搜索文件夹。我添加了
target\u include\u目录(log\u lib log/include)
,但我有错误target\u include\u目录,调用时使用了无效参数
在获得
后担心接口/PUBLIC/PRIVATE-不,你必须用其中一个。@KamilCuk:拍额头是的,你是对的。我的记忆欺骗了我,告诉我默认情况下它是公开的。