0

Disclaimer: I'm new to cmake so i have no idea what I'm doing.

All guides and tutorials i find seem to think I'm running a 20 man team that needs to work together.

All I'm trying to do is put all my libraries in an include folder and lib folder.

In visual studio and code::blocks i just set up the linker in the IDE, but cmake is a little hard for me to wrap my head around.

I've set include and link directories with

include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)

Then i try to

target_link_libraries(${PROJECT_NAME} jsoncpp)

This however produces undefined references. Is there a simple way to add and link libraries without me having to google and add a cmake module for each one?

Edit:

attempts:

target_link_libraries(${PROJECT_NAME} libjsoncpp.a)

error: undefined reference

target_link_libraries(${PROJECT_NAME} libjsoncpp)

error: cannot find -llibjsoncpp

target_link_libraries(${PROJECT_NAME} jsoncpp)

error: undefined reference

target_link_libraries(${PROJECT_NAME} C:/Repos/prosjekt-bad-racoon/lib/libjsoncpp.a)

error: undefined reference

target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a)

error: undefined reference

Edit: Full cmake file

cmake_minimum_required(VERSION 3.8)
project(prosjekt_bad_racoon)

set(CMAKE_CXX_STANDARD 11)

include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)

set(SOURCE_FILES main.cpp Core.cpp Core.h Collider.cpp Collider.h Player.cpp Player.h Texture.cpp Texture.h AudioController.cpp AudioController.h)
add_executable(prosjekt_bad_racoon ${SOURCE_FILES})

set(EXECUTABLE_NAME ${PROJECT_NAME})

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})

target_link_libraries(${PROJECT_NAME} libjsoncpp.a)


find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
    link_directories("${PROJECT_SOURCE_DIR}")
endif()

Include folder

Lib folder

"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Repos\prosjekt-bad-racoon\cmake-build-debug --target prosjekt_bad_racoon -- -j 2
[ 14%] Linking CXX executable prosjekt_bad_racoon.exe
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreC2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:3: undefined reference to `Json::Value::Value(char const*)'
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreD2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:8: undefined reference to `Json::Value::~Value()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\prosjekt_bad_racoon.dir\build.make:236: recipe for target 'prosjekt_bad_racoon.exe' failed
mingw32-make.exe[3]: *** [prosjekt_bad_racoon.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/prosjekt_bad_racoon.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/prosjekt_bad_racoon.dir/rule] Error 2
Makefile:117: recipe for target 'prosjekt_bad_racoon' failed
mingw32-make.exe: *** [prosjekt_bad_racoon] Error 2
Sniffleeu
  • 49
  • 8
  • If you have all libraries in `${PROJECT_SOURCE_DIR}/lib` directory, just add all of them into `target_link_libraries` call (either manually, or using `file(GLOB)`). – Tsyvarev Oct 31 '17 at 19:59
  • I have tried adding them to `target_link_libraries` I even tried giving full path with `target_link_libraries(prosjekt_bad_racoon ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a)` – Sniffleeu Oct 31 '17 at 20:12
  • Undefined reference means that all **libraries** specified are **found**, but the missed symbol is contained in **some other library**. [That is, a linker has found `libjsoncpp`, but the symbol is contained in some other library, against which you should link too]. – Tsyvarev Oct 31 '17 at 20:43
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Tsyvarev Oct 31 '17 at 20:44
  • I changed `include_directories` to include /json at the end and its building. Is there a way to add all subfolders in /include/ or do i need to type them all out? – Sniffleeu Oct 31 '17 at 20:50
  • `Is there a way to add all subfolders in /include/` - If you need that, then something is wrong in your setup or usage. Normally it is sufficient to include `include/` directory. – Tsyvarev Oct 31 '17 at 20:55
  • nevermind, I had removed the code that used jsoncpp while testing something else. When i added it again it still gives undefined reference – Sniffleeu Oct 31 '17 at 20:55
  • It is difficult to suggest something without viewing the code and precise error message. – Tsyvarev Oct 31 '17 at 20:56
  • Added images of folders and full cmake file – Sniffleeu Oct 31 '17 at 21:02
  • Add also precise error message about "undefined reference". – Tsyvarev Oct 31 '17 at 21:03

0 Answers0