C++项目引入RapidJSON头文件库后无法找到头文件的编译问题求助
我最近在自己的C++项目里引入了RapidJSON这个头文件库,之前项目编译完全正常,结果加了之后一直报找不到头文件的错误,折腾了好一阵都没解决,来求助各位大佬!
先给大家看下我原本的项目目录结构:
Application -Utils -Transport -include -Foo.h -src Foo.cpp CMakelists.txt(a) -CMakeLists.txt(b) -third_party -argparse -rapidjson (新添加的) -bin -include ... -CMakeLists.txt -CMakeLists.txt(c)
下面是各个CMake文件的内容:
CMakelists.txt(a)(Transport/src下的CMake):
add_library(${PROJECT_NAME} Foo.cpp ) target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> ) target_link_libraries(${PROJECT_NAME} PUBLIC ... CURL::libcurl RapidJSON <--新添加的依赖 )
CMakelists.txt(b)(Utils/Transport下的CMake):
project(Transport LANGUAGES CXX) add_subdirectory("src")
CMakelists.txt(c)(third_party下的CMake):
add_subdirectory(argparse) add_subdirectory(rapidjson) <--新添加的子目录
我原本的理解是:因为RapidJSON是头文件库,它自带的CMakeLists.txt应该会生成一个叫RapidJSON的接口目标,我只要在需要用到它的目标里链接这个RapidJSON目标,就能自动获取它的头文件路径了。而且看它的CMake配置里确实有include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)这行,那我在Foo.cpp里写#include "rapidjson/document.h"应该是没问题的,但编译时却报错:
fatal error: 'rapidjson/document.h' file not found 1 | #include "rapidjson/document.h" | ^~~~~~~~~~~~~~~~~~~~~~
我还试过在根CMakeLists.txt里加find_package(RapidJSON),结果又出现了这个错误:
CMake Error at cmake-build-debug/third_party/rapidjson/RapidJSONConfig.cmake:3 (include): include could not find requested file:
为了方便大家排查,我特意做了一个最小复现的例子:
最小项目结构:
minimal -thirdparty -argparse (从github下载的源码) -rapidjson(从github下载的源码) -CMakelists.txt(a) -main.cpp -CMakelists.txt(b)
CMakelists.txt(a)(thirdparty下的CMake):
add_subdirectory(argparse) add_subdirectory(rapidjson)
CMakelists.txt(b)(根目录下的CMake):
cmake_minimum_required(VERSION 3.27) project(minimal) add_subdirectory(third_party) set(CMAKE_CXX_STANDARD 17) add_executable(minimal main.cpp) target_link_libraries(minimal PUBLIC argparse RapidJSON )
main.cpp:
#include <iostream> #include <argparse/argparse.hpp> #include "rapidjson/document.h" int main() { std::cout << "Hello, World!" << std::endl; return 0; }
编译这个最小项目时,同样报找不到RapidJSON头文件的错误:
/Users/foo/CLionProjects/minimal/main.cpp:3:10: fatal error: 'rapidjson/document.h' file not found 3 | #include "rapidjson/document.h" | ^~~~~~~~~~~~~~~~~~~~~~ 1 error generated.
同样都是头文件库,argparse就能正常找到头文件,RapidJSON却不行,实在搞不清楚哪里出问题了,有没有大佬能给我指个方向?
备注:内容来源于stack exchange,提问作者bourne

