使用 cmake 将 header-only 库包含到我的项目时出错

error when including header-only library to my project using cmake

我正在尝试将此 header-only 库添加到我的项目中:https://github.com/CPPAlliance/url

我的项目结构:

├── build
│   ├── build.ninja
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.18.0-rc1
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   └── CompilerIdCXX
│   │   ├── cmake.check_cache
│   │   ├── CMakeError.log
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── cmake.verify_globs
│   │   ├── hftbot.dir
│   │   │   ├── build.make
│   │   │   ├── cmake_clean.cmake
│   │   │   ├── CXX.includecache
│   │   │   ├── DependInfo.cmake
│   │   │   ├── depend.internal
│   │   │   ├── depend.make
│   │   │   ├── flags.make
│   │   │   ├── link.txt
│   │   │   ├── progress.make
│   │   │   └── src
│   │   ├── Makefile2
│   │   ├── Progress
│   │   │   ├── 1
│   │   │   └── count.txt
│   │   ├── progress.marks
│   │   ├── rules.ninja
│   │   ├── TargetDirectories.txt
│   │   └── VerifyGlobs.cmake
│   ├── cmake_install.cmake
│   ├── compile_commands.json
│   ├── details.json
│   ├── external
│   │   ├── json
│   │   │   ├── CMakeFiles
│   │   │   └── cmake_install.cmake
│   │   └── url
│   │       ├── CMakeFiles
│   │       └── cmake_install.cmake
│   ├── hftbot
│   ├── Makefile
│   └── src
│       ├── CMakeFiles
│       │   ├── CMakeDirectoryInformation.cmake
│       │   └── progress.marks
│       ├── cmake_install.cmake
│       └── Makefile
├── build.sh
├── CMakeLists.txt
├── configure.sh
├── details.json
├── external
│   └── urlib
│       ├── build
│       │   └── Jamfile
│       ├── cmake
│       │   ├── config.cmake.in
│       │   └── toolchains
│       ├── CMakeLists.txt
│       ├── doc
│       │   ├── images
│       │   ├── Jamfile
│       │   ├── javadoc.hpp
│       │   ├── qbk
│       │   ├── README.md
│       │   ├── tools
│       │   └── xsl
│       ├── extra
│       │   ├── include
│       │   └── test_main.cpp
│       ├── include
│       │   └── boost
│       ├── Jamfile
│       ├── LICENSE_1_0.txt
│       ├── meta
│       │   ├── explicit-failures-markup.xml
│       │   └── libraries.json
│       ├── README.md
│       ├── src
│       │   └── src.cpp
│       └── test
│           ├── CMakeLists.txt
│           ├── Jamfile
│           ├── limits
│           ├── unit
│           └── wpt
├── run.sh
└── src
    ├── CMakeLists.txt
    ├── httpClient.cpp
    └── WebsocketClient.cpp

CMakeLists.txt 在根文件夹中:

cmake_minimum_required(VERSION 3.18.0)

project(hftbot)
  
find_package(Boost 1.79.0 REQUIRED COMPONENTS system thread filesystem container)
find_package(Threads REQUIRED)
add_subdirectory(src)

include_directories(SYSTEM external/urlib/include)

set(URLIB_DIRECTORY "external/urlib/include")
set(URLIB_HEADERS ${URLIB_DIRECTORY}/boost/url.hpp ${URLIB_DIRECTORY}/boost/url/src.hpp)
set(SOURCES src/httpClient.cpp)

add_executable(${PROJECT_NAME} ${SOURCES} ${URLIB_HEADERS})
target_include_directories(${PROJECT_NAME} PRIVATE ${URLIB_DIRECTORY})

如您所见,我使用 target_include_directories() 将 header 文件包含到我的项目中。 但这里似乎有问题。

#include <boost/url.hpp>
#include <boost/url/src.hpp>

当我尝试包含这个时,它显示构建错误:

/usr/local/bin/cmake -S/home/user/Desktop/HFTBOT -B/home/user/Desktop/HFTBOT/build --check-build-system CMakeFiles/Makefile.cmake 0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/Desktop/HFTBOT/build
/usr/local/bin/cmake -E cmake_progress_start /home/user/Desktop/HFTBOT/build/CMakeFiles /home/user/Desktop/HFTBOT/build//CMakeFiles/progress.marks
make -s -f CMakeFiles/Makefile2 all
Scanning dependencies of target hftbot
CMake Error: Directory Information file not found
[ 50%] Building CXX object CMakeFiles/hftbot.dir/src/httpClient.cpp.o
/home/user/Desktop/HFTBOT/src/httpClient.cpp:11:10: fatal error: boost/url/src.hpp: No such file or directory
   11 | #include "boost/url/src.hpp"
      |          ^~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/hftbot.dir/build.make:83: CMakeFiles/hftbot.dir/src/httpClient.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:116: CMakeFiles/hftbot.dir/all] Error 2
make: *** [Makefile:104: all] Error 2

我感觉是不是因为可执行文件的位置导致了这个错误?

由于您的源代码不在项目根目录中,而是在 src/ 中,您可能必须使用 ../external/url/include 指定相对路径。 CMake中比较典型的做法是使用绝对路径:

set(URLIB_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/external/urlib/include")

我创建了一个 self-contained 示例,其中包含构建说明:https://github.com/sehe/BoostURLAsSubmodule/tree/main

BoostURLAsSubmodule

Steps to build:

git clone --recurse-submodules https://github.com/sehe/BoostURLAsSubmodule
cd BoostURLAsSubmodule/
cmake -B build .
make -C build/

If you need, override BOOST_ROOT:

BOOST_ROOT=~/custom/boost_1_79_0/ cmake -B build .