2

So I am trying to get SDL2 to work with CLion (So that I can experiment/learn).

My main code is as such:

#include <iostream>
#include <SDL.h>

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

bool init();

SDL_Window* gWindow = NULL;

SDL_Surface* gScreenSurface = NULL;

SDL_Surface* gHelloWorld = NULL;

bool init(){
    bool success = true;
    /*if(SDL_Init(SDL_INIT_VIDEO)<0){
        success = false;
    }
    else{
    }*/

    return success;
}

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

And my CMake file looks like this

cmake_minimum_required(VERSION 3.6)
project(SDL2_Lesson_1)

set(CMAKE_CXX_STANDARD 11)

# includes cmake/FindSDL2.cmake
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})

set(SOURCE_FILES Lesson_1.cpp)

add_executable(SDL2_App ${SOURCE_FILES})
target_link_libraries(SDL2_App ${SDL2_LIBRARY})

set(SOURCE_FILES Lesson_1.cpp)
add_executable(SDL2_Lesson_1 ${SOURCE_FILES})

Also, I have a file FindSDL2.cmake from here in a folder cmake within the project folder. Now, with the files as I have them posted, everything compiles and runs fine. BUT, when I uncomment the commented section within init(), the compilation breaks down and gives me the following error:

Undefined symbols for architecture x86_64:
  "_SDL_Init", referenced from:
      init() in Lesson_1.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [SDL2_Lesson_1] Error 1
make[2]: *** [CMakeFiles/SDL2_Lesson_1.dir/all] Error 2
make[1]: *** [CMakeFiles/SDL2_Lesson_1.dir/rule] Error 2
make: *** [SDL2_Lesson_1] Error 2

Note: Lesson_1.cpp is the file with the main code. Also, this is only part of the error.

jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
Bob Marley
  • 91
  • 8
  • As a side note, you may find it easier to manage your resources using [RAII](http://stackoverflow.com/questions/2321511/) so you don't have to manually check what is and isn't initialized when you're cleaning up or explicitly deal with cleanup when handling exceptions. You can wrap a C-style interface like SDL for RAII [fairly easily](http://stackoverflow.com/questions/39176805/), and you'll wind up with less resource leaks for your trouble. – jaggedSpire Feb 01 '17 at 20:43
  • How much more error is there? Can you reasonably post the rest of it? – jaggedSpire Feb 01 '17 at 20:47
  • @jaggedSpire Sure, I have posted the rest of the error. – Bob Marley Feb 01 '17 at 20:53
  • added the [tag:cmake] tag, since it looks like script's either not finding or linking the proper binaries. – jaggedSpire Feb 01 '17 at 20:59
  • Your second executable (`SDL2_Lesson_1`) is **not linked** with SDL. That is why you get this error. – Tsyvarev Feb 01 '17 at 21:09
  • @Tsyvarev So what this is trying to do is create two executables, and only one of them is actually linked with SDL. Ah, I think I see it, the build I am running is trying to build SDL2_Lesson_1 which isn't linked as you said. So if I added the target_link_libraries again after the last line it should all work yea? – Bob Marley Feb 01 '17 at 21:15
  • Yes, after linking the second executable too all should work. – Tsyvarev Feb 01 '17 at 22:10

1 Answers1

0

Use find_library() instead of find_package()

find_library(SDL2_LIBRARY SDL2 "path/to/your/library_bundle")
find_library(SDL2_App ${SDL2_LIBRARY})
Wagner Patriota
  • 5,494
  • 26
  • 49