Wicelo
Wicelo

Reputation: 2426

What is this linker error (SDL2)?

I have the following code, the goal is to open an SDL window that displays a timer in milliseconds. So I use SDLttf, SDL2 and Getsystemtime() to get the timer. I get those linker errors :

Error   3   error LNK2019: unresolved external symbol _snprintf referenced in function _SDL_main    ...\Source.obj PROJECT
Error   4   error LNK1120: 1 unresolved externals   PROJECT.exe PROJECT

The code :

#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <windows.h>


int main(int argc, char ** argv)
{
    int quit = 0;
    SDL_Event event;
    char timertxt[1024];

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();
    SYSTEMTIME st1, st2;

    GetSystemTime(&st1);

    SDL_Window * window = SDL_CreateWindow("SDL_ttf in SDL2",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 200,150, 0);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);

    TTF_Font * font = TTF_OpenFont("arial.ttf", 25);
    const char * error = TTF_GetError();
    SDL_Color color = { 255, 255, 255 };

    SDL_Surface * surface;
    SDL_Texture * texture;

    int texW = 0, texH = 0;
    SDL_Rect dstrect;

    while (!quit)
    {
        SDL_PollEvent(&event);
        SDL_Delay(1);

        switch (event.type)
        {
        case SDL_QUIT:
            quit = 1;
            break;
        }

        GetSystemTime(&st2);
        snprintf(timertxt, sizeof(timertxt), "%d", st1.wMilliseconds);
        surface = TTF_RenderText_Solid(font, timertxt, color);
        texture = SDL_CreateTextureFromSurface(renderer,surface);
        SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
        dstrect = (SDL_Rect){ 0, 0, texW, texH };

        SDL_RenderCopy(renderer, texture, NULL, &dstrect);
        SDL_RenderPresent(renderer);
    }

    SDL_DestroyTexture(texture);
    SDL_FreeSurface(surface);
    TTF_CloseFont(font);

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    TTF_Quit();
    SDL_Quit();

    return 0;
}

I don't see what is the problem using snprintf in the sme code as SDL.

Upvotes: 0

Views: 202

Answers (1)

Claudi
Claudi

Reputation: 5416

Use _snprintf or _snprintf_s versions instead of snprintf. In Windows it is considered a deprecated POSIX function.

The following POSIX names for functions are deprecated. In most cases, prepending an underscore character gives the standard equivalent name.

Check this link:

http://msdn.microsoft.com/en-us/library/ms235384%28v=vs.90%29.aspx

Upvotes: 3

Related Questions