0

I am working in a project which uses jsoncpp for parsing and cmake for compilation. I added the jsoncpp official git repository as a submodule to my project with git submodule add REPO_URL external/jsoncpp, so as to keep every dependency together.

When running cmake -B out/build, it works normally. But when I do make, I get the following error:

/usr/bin/ld: cannot find -ljsoncpp: No such file or directory.

The files are organized the following way:

- root
    - out/build
    - external
        - jsoncpp (cloned repo)
    - include
        foo.h
        bar.h
    - src
        foo.cpp
        bar.cpp
        main.cpp
    CMakeLists.txt

The CMakeLists.txt is like this:

cmake_minimum_required(VERSION 3.22.1)
project(ants)


# ".cpp" files in folder "src" into cmake variable "SOURCE"
file(GLOB SOURCE "src/*.cpp")

# Executable
add_executable(${PROJECT_NAME} ${SOURCE})

# Directory where cmake will look for include files
include_directories(include)

# Tells cmake to compile jsoncpp
add_subdirectory(external/jsoncpp)
# Tells cmake where to look for jsoncpp include files
target_include_directories(${PROJECT_NAME} 
    PUBLIC external/jsoncpp/include 
)

target_link_libraries(${PROJECT_NAME} jsoncpp)
2
  • Are you sure cloned jsoncpp have its own CmakeLists.txt ? Commented May 31, 2022 at 3:09
  • I think you need to create some library from CmakeLists.txt inside jsoncpp directory using add_library and need to link that using target_link_libraries in your current CmakeLists.txt Commented May 31, 2022 at 3:14

1 Answer 1

1

The jsoncppConfig.cmake defines property INTERFACE_INCLUDE_DIRECTORIES for targets jsoncpp_lib and jsoncpp_lib_static.

You need to query the target property and set it manually:

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

Linking is done via:

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)

Source.

Try this:

cmake_minimum_required(VERSION 3.22.1)
project(ants)

# ".cpp" files in folder "src" into cmake variable "SOURCE"
file(GLOB SOURCE "src/*.cpp")

# Executable
add_executable(${PROJECT_NAME} ${SOURCE})

# Directory where cmake will look for include files
include_directories(include)

# Tells cmake to compile jsoncpp
add_subdirectory(external/jsoncpp)
get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.