1

I am requesting all your help and/or advise on a strange issue I am encountering trying to understand Conan.

First thing first, the error :

A well-known : /bin/ld: cannot find -lnlohmann_json_schema_validator

What I am trying to do : Trying to build the main example of the json-schema-validator, based on nlohmann_json, using cmake and conan package manager.

Links :

The CMakeLists.txt is pretty simple :

cmake_minimum_required(VERSION 3.10)
project(app VERSION 1.0.0 LANGUAGES CXX)
add_executable(${PROJECT_NAME} src/main.cpp)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
   message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
   file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/master/conan.cmake"
                  "${CMAKE_BINARY_DIR}/conan.cmake")
endif()
include(${CMAKE_BINARY_DIR}/conan.cmake)

# Point 1
conan_cmake_run(REQUIRES json-schema-validator/2.1.0 BASIC_SETUP BUILD missing)

target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
# Point 2
target_include_directories(${PROJECT_NAME} PUBLIC "src")

From this point on, I searched and found different pieces of information :

  • https://stackoverflow.com/a/34256630/7135482 : established that a library is not found by /bin/ld at linking, but as I am using default configuration for everything (Conan profile, Cmake, etc) and wanting the library as static during compilation, I am wondering why /bin/ld cannot find the library located in /home/<user>/.conan/data/json-schema-validator/2.1.0/_/_/package/feb79143ef49afa1ac08f157e4ea835476439057/lib... Seems pretty accurate on conan side, from my point of view... why wouldn't it give CMake a correct path when building Makefiles ?
  • I changed the CMakeLists.txt (between "Point 1" and "Point 2"), thinking it was the use of the "unrecommanded" conan_cmake_run(...). So I changed it according to the documentation on Github
conan_cmake_configure(REQUIRES json-schema-validator/2.1.0
                      GENERATORS cmake)

conan_cmake_autodetect(settings)

conan_cmake_install(PATH_OR_REFERENCE .
                    BUILD missing
                    REMOTE conan-center
                    SETTINGS ${settings})

(here I get the error fatal error: nlohmann/json-schema.hpp: No such file or directory. So, it seems this way of doing thing doesn't even #include the files)

  • the only way I got the library and its example to work is by by-passing Conan completely..., by cloning nlohmann_json repo and json-schema-validation repo into a vendor folder and by adding them to CMake system as following :
#add_subdirectory(vendor/json)
#add_subdirectory(vendor/json-schema-validator)

#target_link_libraries(${PROJECT_NAME} nlohmann_json::nlohmann_json nlohmann_json_schema_validator)

But what is the point of trying to make my life easier using a package manager if the only way to make my project build, is to avoid the use of the package manager ?

Does someone already encountered this type of issue ? or have another idea I could try or change ?

Any help will be highly appreciated ! SO, thanks in advance !

mscherer
  • 97
  • 1
  • 3
  • 12

1 Answers1

0

I don't know where you found a tutorial, example or explanation, but you should read the official cmake-conan README, which contains a functional example.

Said that, your case can be fixed by the following example:

cmake_minimum_required(VERSION 3.5)
project(validator CXX)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})

if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
  message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
  file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/v0.16.1/conan.cmake"
                "${CMAKE_BINARY_DIR}/conan.cmake"
                EXPECTED_HASH SHA256=396e16d0f5eabdc6a14afddbcfff62a54a7ee75c6da23f32f7a31bc85db23484
                TLS_VERIFY ON)
endif()

include(${CMAKE_BINARY_DIR}/conan.cmake)

conan_cmake_configure(REQUIRES json-schema-validator/2.1.0
                      GENERATORS cmake_find_package)

conan_cmake_autodetect(settings)

conan_cmake_install(PATH_OR_REFERENCE .
                    BUILD missing
                    REMOTE conan-center
                    SETTINGS ${settings})

find_package(nlohmann_json_schema_validator REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} nlohmann_json_schema_validator::nlohmann_json_schema_validator)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)

Some differences here, I'm using CMake targets instead of CMake variable CONAN_LIBS, and I asked for cmake_find_package generator. It's a modern CMake approach.

uilianries
  • 3,363
  • 15
  • 28
  • 1
    Thanks for your answer ! The example I was refering to, is this [one](https://github.com/pboettch/json-schema-validator#code) :) But following your CMakeLists.txt, I noticed mine was lacking a `list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})`, so generated Find*.cmake could not be found. I retried with `GENERATORS cmake` , but still gives me a "missing header" error. I won't worry about this, using `cmake_find_package` seems to work fine with the missing `APPEND` line, so, for now, I'll stick with this :) Thanks ! – mscherer Apr 20 '21 at 07:07