0

I'm currently learning CMake and I'm trying to create my first test project. I'm able to get a simple project up and running in visual studio via CMake. However, I'm having trouble trying to add a library. I've read some guides and things but I keep getting errors. Basically, I'm trying to link SDL libraries (a game programming library) in my sample project. I've placed these libraries in a top level, 'ThirdParty' folder. Here is what my CmakeLists.txt file looks like in my top level directory:

cmake_minimum_required(VERSION 2.8.11)

project(Hello)

#Find necessary header files
find_path(SDL_INCLUDE_DIR SDL.h HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/include/)

#Find necessary library files
find_library(SDL_LIB_DIR SDL2 HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
find_library(SDLMAIN_LIB_DIR SDLmain HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)

#Add/Link files to project
include_directories(${SDL_INCLUDE_DIR})
target_link_libraries(Test PUBLIC ${SDL_LIB_DIR})
target_link_libraries(Test PUBLIC ${SDLMAIN_LIB_DIR})



add_executable(Test "${CMAKE_SOURCE_DIR}/Source/Main.cpp")

I'm not 100 percent sure of the HINTS parameter, but I saw it used on another thread. Anyway, here's the error I keep getting:

CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
SDLMAIN_LIB_DIR
    linked by target "Test" in directory C:/Users/Jason/Desktop/Test

What am I doing wrong and how do I properly link libraries in CMake?

Jason
  • 2,198
  • 3
  • 23
  • 59
  • As you can see from the error message, the problem is not with *linking* `SDLmain` library, but with **searching** it - it is `find_library()` who sets *NOTFOUND* value when it cannot find the library requested. See [that question](http://stackoverflow.com/questions/14243524/cmake-find-library-matching-behavior) about how `find_library` works and how to find your library with it. – Tsyvarev Apr 09 '17 at 10:30

1 Answers1

2
  1. In cmake, you first create the executable, and then you link it to a library
  2. You have to understand how finding libraries and packages works in CMake. Typically the way it works is that you use find_library or find_package, and then cmake will set some variables that you can use to link to/use the library.

I'm not familiar with SDL, but by googling a little bit about it, I would say this is how it should look like:

find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(MyExec main.cpp)
target_include_directories(MyExec ${SDL2_INCLUDE_DIR})
target_link_libraries(MyExec ${SDL2_LIBRARY})

That find_library will set the variables SDL2_INCLUDE_DIR and SDL2_LIBRARY, which you can use to link to SDL and add its includes to your project.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • So if I follow your steps and take out the target_link_libraries call to SDLmain then I can get the program to work. However, if I try and enter back in SDLmain, CMake keeps telling me it can't be found. The script I'm using is this: `find_library(SDLMAIN_LIB_DIR LIBRARY NAME SDL2main)` and this : `target_link_libraries(Test ${SDLMAIN_LIB_DIR})`. Done in the same manner as the other link library. Is there something I'm doing wrong in CMake or is it most likely an SDL thing? – Jason Apr 09 '17 at 13:37
  • @Jason I recommend that you start by trying to link manually with SDL libraries, not through CMake automated tools (like `find_library`). Once you succeed, retreat and start using the fancy CMake tools step by step. – The Quantum Physicist Apr 09 '17 at 13:42
  • Thats the thing, I have and when I did I was able to both libraries for my program. CMake just keeps telling me that the variable SDLMAIN_LIB_DIR cannot be found. – Jason Apr 09 '17 at 13:43
  • @Jason Usually CMake has a variable that indicates its success. In this case it's `SDL_FOUND` I think. Look here: https://cmake.org/cmake/help/v3.0/module/FindSDL.html . Does it find SDL? Is that variable `true`/`ON`? – The Quantum Physicist Apr 09 '17 at 13:52
  • @Quantum Physicist Hmm. Well, I'll look into what your talking about. As I mentioned earlier I'm still pretty new to Cmake and not quite sure how to properly utilize all its tools. I'll see what I can find and then make another post if I'm still having trouble. Thanks for the help. – Jason Apr 09 '17 at 16:01
  • @Jason One last thing: use `message()` to print anything you want. It's good for basic debugging. – The Quantum Physicist Apr 09 '17 at 16:12
  • @Quantum Physicist Fantastic. Thanks for the heads up. – Jason Apr 09 '17 at 16:34