Petr Shypila
Petr Shypila

Reputation: 1509

CMake: Library not found -ljsoncpp

I'm very new in C++ and CMake.

In my project I use jsoncpp library and my IDE(CLion) see it without any problems. However when I try to compile it I get this error message:

ld: library not found for -ljsoncpp

Here is my project structure:

/
|-jsoncpp/  /*Here contains source code, not compiled library*/
|
|-work_7/
|  |-main.cpp
|
|-CMakeList.txt

Here is CMakeList.txt config:

cmake_minimum_required(VERSION 2.8.4)
project(programming_practice)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES work_7/main.cpp)

add_executable(programming_practice ${SOURCE_FILES})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/include)
add_subdirectory(jsoncpp)

target_link_libraries(programming_practice jsoncpp)

So what I did wrong? Please help me.

Upvotes: 1

Views: 7038

Answers (2)

DWilches
DWilches

Reputation: 23035

You also need to specify where the library for jsoncpp is located. Add this to your CMakeLists.txt:

link_directories(${CMAKE_CURRENT_SOURCE_DIR}/jsoncpp/lib)

That path should be the one containing the file named: libjsoncpp.o or the equivalent in your S.O.

Upvotes: 5

qqibrow
qqibrow

Reputation: 3022

The problem is that you need to compile jsoncpp first to a library before using it. Use add_subdirectory(jsoncpp) and make sure you have Cmake file in that directory for compilation.

Upvotes: 2

Related Questions