我有以下代码,目标是打开一个以毫秒为单位显示计时器的 SDL 窗口。所以我使用 SDLttf、SDL2 和 Getsystemtime() 来获取计时器。我收到这些链接器错误:
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
代码:
#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;
}
我不明白在 SME 代码中使用 snprintf 作为 SDL 有什么问题。
最佳答案
使用 _snprintf
或 _snprintf_s
版本代替 snprintf
。在 Windows 中,它被认为是已弃用的 POSIX 函数。
The following POSIX names for functions are deprecated. In most cases, prepending an underscore character gives the standard equivalent name.
检查此链接:
http://msdn.microsoft.com/en-us/library/ms235384%28v=vs.90%29.aspx
关于c - 这个链接器错误(SDL2)是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24989889/