0
$\begingroup$

I am getting

C:\Users\rasmu\AppData\Local\Temp\ccwOn3mM.o:main.c:(.text+0x11f): undefined reference to `SDL_setRenderDrawColor'

C:\Users\rasmu\AppData\Local\Temp\ccwOn3mM.o:main.c:(.text+0x155): undefined reference to `SDL_setRenderDrawColor'

when i try to compile my basic SDL2 c program. My code is

#define ASSERT(_e, ...) if (!(_e)) { fprintf(stderr, __VA_ARGS__); exit(1); }
#include <SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>


typedef float f32;
typedef double f64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;


const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
const int FPS = 30;

typedef struct {
    SDL_Window* window;
    SDL_Texture* texture;
    SDL_Renderer* renderer;
    u32* pixels;
    bool quit;
} state;

u32* init_pixels();
void put_pixel(state state);

int main( int argc, char* args[]){
    state state;
    ASSERT(
        !SDL_Init(SDL_INIT_VIDEO),
        "SDL failed to init: %s\n",
        SDL_GetError()
    );
    state.window = 
        SDL_CreateWindow(
            "Game",
            SDL_WINDOWPOS_CENTERED_DISPLAY(1),
            SDL_WINDOWPOS_CENTERED_DISPLAY(1),
            1280,
            720,
            SDL_WINDOW_ALLOW_HIGHDPI
        );
        
    ASSERT(state.window, "failed to create SDL window: %s\n", SDL_GetError());
    state.pixels = malloc(SCREEN_WIDTH * SCREEN_HEIGHT * 4);

    state.renderer =
        SDL_CreateRenderer(
            state.window,
            -1, 
            NULL
        );

    SDL_setRenderDrawColor(state.renderer,0,0,0,255);
    SDL_RenderClear(state.renderer);
    SDL_setRenderDrawColor(state.renderer,255,255,255,255);
    SDL_RenderDrawPoint(state.renderer,SCREEN_WIDTH/2,SCREEN_HEIGHT/2);
    SDL_RenderPresent(state.renderer);

    while(!state.quit){
        SDL_Event ev;
        while(SDL_PollEvent(&ev)){
            switch(ev.type){
                case SDL_QUIT:
                    state.quit = true;
                    break;
            }
        }
    }


        
    
    SDL_DestroyWindow(state.window);
    free(state.pixels);
    SDL_QUIT;
    return 0;
}

And my make file looks like this

OBJS = main.c

CC = gcc

INCLUDE_PATHS = -IC:\mingw_dev_lib\include\SDL2

LIBRARY_PATHS = -LC:\mingw_dev_lib\lib

COMPILER_FLAGS = -w -Wl,-subsystem,windows

LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2 

OBJ_NAME = game

all : $(OBJS)
$(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) -o $(OBJ_NAME) $(LINKER_FLAGS)

I do believe it has something to do with the linking of the SDL2 library but it seems weird since the SDL.h works fine but only when I have tried to use the SDL_setRenderDrawColor() function. I am quite new to SDL and linking libraries.

$\endgroup$
2
  • 3
    $\begingroup$ 'undefined reference' is always a linker error. Means the linker doesn't find the SDL library. Headers of libraries usually only tell the compiler what is available, not how it is implemented. So every time you use a function deklared in the header, the compiler "leaves a note for the linker to fill in the missing bits". If the compiler doesn't write such a "note", because you didn't use a function from the header, the linker has nothing to complain about. Long story short, check the make file. Further suggestion: use "cmake" in the future instead of "make" $\endgroup$
    – wychmaster
    Mar 8, 2023 at 7:39
  • $\begingroup$ @wychmaster must be right, the casing conversion used by SDL is uppercased words. Check the function name in the header file. $\endgroup$
    – user1703
    May 15, 2023 at 13:18

1 Answer 1

1
$\begingroup$

Looks like a typo since the compiler isn't complaining about the other SDL function calls. You probably need to change SDL_setRenderDrawColor to SDL_SetRenderDrawColor

https://wiki.libsdl.org/SDL2/SDL_SetRenderDrawColor

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.