Search code examples
ccmakesdlclionundefined-reference

How do I link libraries in Clion using CMake?


I am trying to display graphics in C and for that I want to link the SDL libraries, but this is the first time I've done something like this and everything I've found has been unhelpful.

What I figured out is that I apparently need to link the libraries in CMake, but I saw completely different ways people did it and none of them seems to work for me.

I'm on windows, if that's important

This is my current CMake file:

cmake_minimum_required(VERSION 3.26)
project(SDLtest C)

set(CMAKE_C_STANDARD 11)

add_executable(SDLtest main.c)

target_include_directories (SDLtest PUBLIC include/SDL2)
find_library(SDL_LIBRARY SDLtest lib)
target_link_libraries(SDLtest LINK_PUBLIC ${TEST_LIBRARY})

This is the test code I'm using:

#include <stdio.h>
#include "SDL.h"

int SDL_main(int argc, char* argv[]){
    SDL_Init(SDL_INIT_EVERYTHING);   // Initialize SDL2
    SDL_Window *window;        // Declare a pointer to an SDL_Window
    // Create an application window with the following settings:
    window = SDL_CreateWindow(
            "An SDL2 window",                  //    window title
            SDL_WINDOWPOS_UNDEFINED,           //    initial x position
            SDL_WINDOWPOS_UNDEFINED,           //    initial y position
            640,                               //    width, in pixels
            480,                               //    height, in pixels
            SDL_WINDOW_SHOWN|SDL_WINDOW_OPENGL //    flags - see below
    );// Check that the window was successfully made
    if(window==NULL){
        // In the event that the window could not be made...
        printf("could not create window: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Event event;
    while(1){
        SDL_WaitEvent(&event);
        switch (event.type) {
            case SDL_QUIT : SDL_DestroyWindow(window); SDL_Quit(); break;
            default : break;
        }
    }
}

These are the kind of errors I get (not all the errors because they are the same and it would just take up space):

C:\Program Files\JetBrains\CLion 2023.2.1\bin\mingw\bin/ld.exe: C:/Projects/SDLtest/main.c:25: undefined reference to `SDL_DestroyWindow'

C:\Program Files\JetBrains\CLion 2023.2.1\bin\mingw\bin/ld.exe: C:/Projects/SDLtest/main.c:25: undefined reference to `SDL_Quit'

C:\Program Files\JetBrains\CLion 2023.2.1\bin\mingw\bin/ld.exe: C:/Program Files/JetBrains/CLion 2023.2.1/bin/mingw/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text+0x130): undefined reference to `WinMain'

And this is my file structure: clion file structure

If I missed anything important it's because I don't know about it, any help would be appreciated


Solution

  • I got it working with some help, here's what I needed to do:

    replaced

    find_library(SDL_LIBRARY SDLtest lib)
    

    with

    find_library(SDL_LIBRARY NAMES libSDL2 libSDL2main HINTS "${CMAKE_CURRENT_SOURCE_DIR}/lib")
    

    in my cmake file, some explanation on that:


    SDL_LIBRARY is the name of the variable that I'm creating here, this is what I'll be putting into targer_link_libraries() (refer back to the question), then NAMES are the names of the library files I need, in this case libSDL2 and libSDL2main, after HINTS is the path to these files, ${CMAKE_CURRENT_SOURCE_DIR} gets the path to the directory that the CMake file is in, and in my case the libraries are in the /lib subdirectory of that


    this is where the fuckery sets in, this got my CMake file to work but my code still didn't compile because of an error stating: undefined reference to WinMain() this was fixed by renaming my int SDL_main(int argc, char* argv[]) function to int WinMain(int argc, char* argv[]) but I quite honestly do not understand this part in any way, so idk if this will result in problems later or what causes this.