0

My system is MacOS Mojave. I'm new to SDL2 and I already installed it successfully in XCode and tested main.c. I'm sure it is OK. As I'm using Clion mostly I'd like to handle this issue and continue using it. I installed SDL2 with brew as there was no other possibility to do this. So the way it is installed in is: /Users/Username/.brew/Cellar/SDL2/2.0.12_1

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(SDL_tutorial C)

set(CMAKE_C_STANDARD 99)

find_library(SDL2_LIBRARY NAME SDL2)
add_executable(SDL_tutorial main.c)
include_directories(SDL_tutorial ${SDL2_INCLUDE_DIRS})
target_link_libraries(SDL_tutorial ${SDL2_LIBRARIES})

Here is the output:

[ 50%] Building C object CMakeFiles/SDL_tutorial.dir/main.c.o
[100%] Linking C executable SDL_tutorial
Undefined symbols for architecture x86_64:
  "_SDL_CreateWindow", referenced from:
      _main in main.c.o
  "_SDL_DestroyWindow", referenced from:
      _main in main.c.o
  "_SDL_FreeSurface", referenced from:
      _main in main.c.o
  "_SDL_GetError", referenced from:
      _main in main.c.o
  "_SDL_GetWindowSurface", referenced from:
      _main in main.c.o
  "_SDL_Init", referenced from:
      _main in main.c.o
  "_SDL_LoadBMP_RW", referenced from:
      _main in main.c.o
  "_SDL_PollEvent", referenced from:
      _main in main.c.o
  "_SDL_Quit", referenced from:
      _main in main.c.o
  "_SDL_RWFromFile", referenced from:
      _main in main.c.o
  "_SDL_UpdateWindowSurface", referenced from:
      _main in main.c.o
  "_SDL_UpperBlit", referenced from:
      _main in main.c.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]: *** [SDL_tutorial] Error 1
make[2]: *** [CMakeFiles/SDL_tutorial.dir/all] Error 2
make[1]: *** [CMakeFiles/SDL_tutorial.dir/rule] Error 2
make: *** [SDL_tutorial] Error 2

And my main.c

#include <stdio.h>
#include </Users/Username/.brew/Cellar/sdl2/2.0.12_1/include/SDL2/SDL.h>

int main(int argc, const char * argv[])
{
    SDL_Surface *imageSurface = NULL;
    SDL_Surface *windowSurface = NULL;

    const int WIDTH = 800, HEIGHT = 600;
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
        printf("SDL not initialized: %s\n", SDL_GetError());
    SDL_Window *window = SDL_CreateWindow("HELLO WORLD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
    windowSurface = SDL_GetWindowSurface(window);
    if (!window)
        printf("SDL window not created: %s", SDL_GetError());
    SDL_Event windowEvent;

    imageSurface = SDL_LoadBMP("julia_set.bmp");

    if (imageSurface == NULL)
        printf("SDL could not load image: %s\n", SDL_GetError());

    while (1)
    {
        if (SDL_PollEvent(&windowEvent))
        {
            if (SDL_QUIT == windowEvent.type)
                break;
        }
        SDL_BlitSurface(imageSurface, NULL, windowSurface, NULL);
        SDL_UpdateWindowSurface(window);
    }
    SDL_FreeSurface(windowSurface);
    imageSurface = NULL;
    windowSurface = NULL;
    SDL_DestroyWindow(window);
    SDL_Quit();
    return EXIT_SUCCESS;
    //printf("Hello, World!\n");
}
dgruyere
  • 1
  • 1
  • 1
    Instead of find_library you want find_package as in this example: [https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/](https://trenki2.github.io/blog/2017/06/02/using-sdl2-with-cmake/) – drescherjm Aug 05 '20 at 17:28
  • That worked. Thanks a lot! – dgruyere Aug 06 '20 at 14:28

0 Answers0