0

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:

  1. Make all files in a folder accessible to all projects (subdirectories)
  2. How To Configure CMakeLists.txt To Make C++ Project?
  • "Rapidjson headers CANNOT be accessed in `/root/cpp-library/include` (`testClass1.h` and `testClass2.h`)" - You cannot add include directories for **headers** (`.h`), you add include directories for **source files** (`.cpp`). When a source file is compiled, a compiler will resolve all its `#include` directives according to include directories. The same resolution is performed for `#include` directives in headers, included in the given source file. So, which **source file** is compiled when a compiler cannot resolve rapidjson header? Please, **add exact error message** into the question post. – Tsyvarev Dec 19 '22 at 12:08
  • Added exact error message in the edit section of the post. Thank you for your help so far. – Fish_In_A_Suit Dec 19 '22 at 12:48
  • The error message should include also name of the compiled **source file**, not just a name of the header file which `#include` directive cannot be resolved. Header files normally are not compiled by themselves (and adding a header file to `add_library` call doesn't compile that file: https://stackoverflow.com/questions/36174499/why-add-header-files-into-add-library-add-executable-command-in-cmake) – Tsyvarev Dec 19 '22 at 14:22
  • Please remove excessive whitespace from your code indentation. See [What should I keep out of my posts and titles?](https://meta.stackexchange.com/q/131009/997587) – starball Dec 19 '22 at 22:29

0 Answers0