ArthurSonzogni / nlohmann_json_cmake_fetchcontent

A lightweight Release-tracking repository for nlohmann/json. Suitable for CMake fetchcontent. Automatically upgraded every weeks.
MIT License
79 stars 25 forks source link

Cant seem to include library with cmake fetch. #4

Closed dominique120 closed 3 years ago

dominique120 commented 3 years ago

I'm trying to build an application with this library but either I'm making a mistake(most likely) or cmake fetch is not working.

I copied the code section exactly as it is in the readme and I can see that it downloads the repo, creates the files and headers but the compiler cant seem to find the header.

What could I be doing wrong?

Here is my cmake file as it is now:

cmake_minimum_required(VERSION 3.10)

set(CMAKE_DISABLE_SOURCE_CHANGES ON)
set(CMAKE_DISABLE_IN_SOURCE_BUILD ON)

project(vitanza-service CXX)

add_subdirectory(VitanzaService)
add_executable(vts ${vts_SRC})

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

set_target_properties(vts PROPERTIES CXX_STANDARD 14)
set_target_properties(vts PROPERTIES CXX_STANDARD_REQUIRED ON)

include(FetchContent)
LINK_DIRECTORIES(/usr/local/lib)

FetchContent_Declare(json
  GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent.git
  GIT_TAG v3.9.1)

  FetchContent_GetProperties(json)
if(NOT json_POPULATED)
  FetchContent_Populate(json)
  add_subdirectory(${json_SOURCE_DIR} ${json_BINARY_DIR} EXCLUDE_FROM_ALL)
endif()

if (NOT WIN32)
    add_compile_options(-Wall -Werror -pipe -fvisibility=hidden)
endif ()

set(CMAKE_CXX_FLAGS_PERFORMANCE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")

if (CMAKE_COMPILER_IS_GNUCXX)
    add_compile_options(-fno-strict-aliasing)
endif ()

find_package(AWSSDK REQUIRED COMPONENTS dynamodb)
find_package(MySQL REQUIRED)
find_package(Boost 1.53.0 REQUIRED COMPONENTS system )

include_directories(${Boost_INCLUDE_DIRS} ${MYSQL_INCLUDE_DIR} ${json_SOURCE_DIR})

target_link_libraries(vts PRIVATE
        Boost::system
        ${AWSSDK_LINK_LIBRARIES}
        ${MYSQL_CLIENT_LIBS}
        nlohmann_json::nlohmann_json
        served libserved.so
        )

I also tried downloading, building and installing the library manually and using find_package(nlohmann_json 3.2.0 REQUIRED) and it will find the package and begin compiling but it also failed to find the include files.

Also tried adding /include after ${json_SOURCE_DIR}

CMake shows no errors, I'm on CentOS 8 and using GCC 8. Thanks!

ArthurSonzogni commented 3 years ago

I recommend reading: https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

It's hard for me to predict the error. Try to replace:

include_directories(${Boost_INCLUDE_DIRS} ${MYSQL_INCLUDE_DIR} ${json_SOURCE_DIR})

by

target_include_directories(vts PRIVATE ${Boost_INCLUDE_DIRS})
target_include_directories(vts PRIVATE ${MYSQL_INCLUDE_DIR})

(I purposefully didn't added anything about ${json_SOURCE_DIR}, this is useless, and this might be the problem.

dominique120 commented 3 years ago

Its an interesting article thanks! However I solved the issue. There was nothing wrong with my cmake file. The issue was that I was using #include <nlohmann\json.hpp> instead of #include <nlohmann/json.hpp> and linux does not recognize backslashes as directory separators.