无法使用柯南找到图书馆

Cannot find library using Conan

我请求你的所有帮助and/or就我在试图理解柯南时遇到的一个奇怪问题提出建议。

首先,错误:

一个众所周知的:/bin/ld: cannot find -lnlohmann_json_schema_validator

我想做什么: 尝试构建 json-schema-validator 的主要示例,基于 nlohmann_json,使用 cmake 和 conan 包管理器。

链接:

CMakeLists.txt 非常简单:

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")

从这里开始,我搜索并找到了不同的信息:

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})

(这里我得到错误 fatal error: nlohmann/json-schema.hpp: No such file or directory。所以,似乎这种做事的方式甚至没有 #include 文件)

#add_subdirectory(vendor/json)
#add_subdirectory(vendor/json-schema-validator)

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

但是,如果构建项目的唯一方法是避免使用包管理器,那么尝试使用包管理器让我的生活更轻松的意义何在?

有人遇到过此类问题吗?或者有其他我可以尝试或更改的想法?

非常感谢任何帮助!所以,提前致谢!

我不知道你在哪里找到教程、示例或解释,但你应该阅读官方的 cmake-conan README,其中包含一个功能示例。

也就是说,您的情况可以通过以下示例解决:

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)

这里有些不同,我使用 CMake 目标而不是 CMake 变量 CONAN_LIBS,并且我要求 cmake_find_package 生成器。这是一种现代的 CMake 方法。