代码之家  ›  专栏  ›  技术社区  ›  dopller

使用cmake将仅标题库包含到我的项目时出错

  •  1
  • dopller  · 技术社区  · 2 年前

    我正在尝试将此仅标题库添加到我的项目中: 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() 将头文件包含到我的项目中。 但这里似乎有点不对劲。

    #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
    

    我有一种感觉,是因为可执行文件的位置导致了这个错误吗?

    1 回复  |  直到 2 年前
        1
  •  0
  •   sehe    2 年前

    因为您的源不在项目根目录中,而是在 src/ 您可能必须使用 ../external/url/include .CMake中更典型的方法是使用绝对路径:

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