skip to Main Content

Having some trouble right now working with conan2, cmake and the jsoncpp library. I’ve tried to make the jsoncpp a shared library. I’ve tried using the jsoncpp/json/json.h path. I don’t want to run the command sudo apt install libjsoncpp because that won’t allow me to keep this easy to work with cross platform.

The two problems I have:
I don’t know if I’m properly linking the library correctly because I’m getting errors like this on build undefined reference to Json::Value::Value(Json::ValueType)'. The full command script I am running is:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Debug
cd ./build && make && cd ..

The second problem is I don’t know if I’m importing the library the right way.

main.cpp

#include <json/json.h>
#include <iostream>
#include <fstream>

int main() {
  std::ifstream file("test.json");
  Json::Value root;
  file >> root;
  std::cout << root["title"] << std::endl;
  return 0;
}

CMakeList.txt

cmake_minimum_required(VERSION 3.18.4)

message("Building with CMake version: ${CMAKE_VERSION}")

project(Yediri)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_COMPILER "g++")
add_definitions("-std=c++20")

add_executable(${PROJECT_NAME} main.cpp)
message("Project Name: ${PROJECT_NAME}")

find_package(jsoncpp REQUIRED)
target_link_libraries(${PROJECT_NAME} ${jsoncpp_LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC ${jsoncpp_INCLUDE_DIRS})

message("FOUND CONFIG: ${jsoncpp_CONFIG}")
message("INCLUDE DIRS: ${jsoncpp_INCLUDE_DIRS}")
message("LIBRARIES: ${jsoncpp_LIBRARIES}")


install(TARGETS Yediri DESTINATION bin)

conanfile.txt

[requires]
jsoncpp/1.9.5

[generators]
CMakeDeps
CMakeToolchain

Message output:

FOUND CONFIG: /home/user/yediri/build/jsoncpp-config.cmake
INCLUDE DIRS: /home/user/.conan2/p/jsonc925d41328bf9d/p/include
LIBRARIES: JsonCpp::JsonCpp

2

Answers


  1. Chosen as BEST ANSWER

    So I figured out what was going on. My version of CMake is older than what conan anticipates. cmake version 3.18.4. Conan expects a version > 3.20. So the fix was to add the following configuration to my cmake command.

    cmake -S . -B build -G "Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=/home/jfehrman/yediri/build/conan_toolchain.cmake -DCMAKE_POLICY_DEFAULT_CMP0091=NEW -DCMAKE_BUILD_TYPE=Release
    

    When I read the output of conan install more closely it advised how to configure CMake for versions earlier than 3.20. That is how I found the answer.


  2. Use CMake imported targets like a good citizen, and keep it simple:

    cmake_minimum_required(VERSION 3.18)
    project(Yediri LANGUAGES CXX)
    
    find_package(jsoncpp REQUIRED)
    
    add_executable(Yediri main.cpp)
    target_compile_features(Yediri PRIVATE cxx_std_20)
    target_link_libraries(Yediri PRIVATE JsonCpp::JsonCpp)
    
    include(GNUInstallDirs)
    install(TARGETS Yediri DESTINATION ${CMAKE_INSTALL_BINDIR})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search