I am trying to make #include <rapidjson/document.h> and other rapidjson headers available from all directories inside a C++ project. Rapidjson was installed successfully using vcpkg.
The project structure is as follows:
/root/
+ CMakeLists.txt
+ CMakePresets.json
+ apps/
+ CMakeLists.txt
+ main.cpp
+ cpp-library/
+ CMakeLists.txt
+ src/
+ testClass1.cpp
+ testClass2.cpp
+ ...
+ include/
+ testClass2.h
+ testClass2.h
+ ...
/root/CMakeLists.txt:
cmake_minimum_required(VERSION 3.11-3.18)
set(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "")
find_package(Rapidjson CONFIG REQUIRED)
project(
AngioDiabetesRegulation
VERSION 0.1
DESCRIPTION "Small CMake example built with VSCode")
include_directories(C:/Aljosa/Development/Unity-Github/vcpkg/installed/x64-windows/include)
# Rapidjson_INCLUDE_DIRS loaded with find_package(Rapidjson) - checked that it loads correctly
# this is the same as C:/Aljosa/Development/Unity-Github/vcpkg/installed/x64-windows/include
include_directories(${Rapidjson_INCLUDE_DIRS})
add_subdirectory(cpp-library)
add_subdirectory(apps)
/apps/CMakeLists.txt
cmake_minimum_required(VERSION 3.11-3.18)
project(main)
add_executable(main main.cpp)
#find_package(Rapidjson CONFIG REQUIRED)
#include_directories(${Rapidjson_INCLUDE_DIRS})
#target_link_libraries(main PRIVATE cpp-library ${Rapidjson_LIBRARIES} rapidjson)
target_link_libraries(main PRIVATE cpp-library)
target_compile_features(main PUBLIC cxx_std_20)
/cpp-library/CMakeLists.txt
cmake_minimum_required(VERSION 3.11-3.18)
set(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "")
project(cpp-library)
#include_directories(C:/Aljosa/Development/Unity-Github/vcpkg/installed/x64-windows/include)
#find_package(RapidJSON REQUIRED)
#include_directories(C:/Aljosa/Development/Unity-Github/vcpkg/installed/x64- windows/include/rapidjson)
add_library(cpp-library
src/testClass1.cpp
src/testClass2.cpp
)
# find_package(Rapidjson CONFIG REQUIRED)
# include_directories(${Rapidjson_INCLUDE_DIRS}) # is the same as below statement
# include_directories(C:/Aljosa/Development/Unity-Github/AngioDiabetesRegulation/rapidjson)
# PUBLIC needed to make both /include/*.h and /src/*.cpp available elsewhere in project
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
# Tell compiler to use C++20 features. The code doesn't actually use any of them.
target_compile_features(cpp-library PUBLIC cxx_std_20)
Rapidjson headers CAN be accessed in the following directories:
/root/apps
(main.cpp) without specifying any include_directories or linking any libraries in /apps/CMakeLists.txt/root/cpp-library/src
(testClass1.cpp and testClass2.cpp)
Rapidjson headers cannot be accessed in /root/cpp-library/include
(testClass1.h and testClass2.h) even after specifically including the path to rapidjson's includes in the /root/cpp-library/CMakeLists.txt
.
What should I do to make rapidjson headers accessible in root/cpp-library/include
(that contains .h files) also?
EDIT #1: I am guessing that the /root/cpp-library/include/*.h files cannot access rapidjson, because they are not included in CMakeLists to the CMakeLists project. If you look at /root/cpp-library/CMakeLists.txt you can only see that only .cpp classes are added to the "project", whereas .h files are not included. Could that be the case?
Exact error: cannot open source file rapidjson/document.h (when trying to include #include <rapidjson/document.h>
into testClass1.h or testClass2.h). Note that the above include works fine in testClass1.cpp and testClass2.cpp.
EDIT #2 I tried adding "include/testClass1.h" into the add_library function of /root/cpp-library/CMakeLists.txt, which resolved the error - rapidjson/document.h is now also found in testClass1.h. But I am wondering, is that good practice to add both .cpp and .h files into the add_library function?
The working /root/cpp-library/CMakeLists.txt then looks like:
cmake_minimum_required(VERSION 3.11-3.18)
set(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "")
project(cpp-library)
add_library(cpp-library
src/testClass1.cpp
src/testClass2.cpp
"include/testClass1.h"
"include/testClass2.h"
)
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_compile_features(cpp-library PUBLIC cxx_std_20)
I have tried to debug the issue for the past two days, following numerous StackOverflow suggestions, but with no success.
Some of the reviewed topics: