使用SDL2库启动窗口并在黑色背景上绘制彩色方块我没有任何问题。但是当我尝试使用SDL2_gfx库绘制圆圈时,fillCircleColor()是未定义的:
make build;
make[1]: Entering directory `/home/mike/compile/sdlTest'
gcc -std=c99 -c sdlTest.c;
gcc sdlTest.o -o sdlTest -I/usr/include/SDL2 -lSDL2;
sdlTest.o: In function `main':
sdlTest.c:(.text+0x176): undefined reference to `filledCircleColor'
collect2: error: ld returned 1 exit status
make[1]: *** [build] Error 1
make[1]: Leaving directory `/home/mike/compile/sdlTest'
make: *** [test] Error 2
我做错了什么?
这是我的C:
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
SDL_Event event;
//SDL2 variables
void* nullptr;
SDL_Window *win;
SDL_Renderer *ren;
int main(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("SDL_Init Error: %s\n", SDL_GetError());
}
win = SDL_CreateWindow("sdlTest", 100, 100, 10*32, 10*32, SDL_WINDOW_SHOWN);
if (win == nullptr) {
printf("SDL_CreateWindow Error: %s\n", SDL_GetError());
}
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (ren == nullptr) {
printf("SDL_CreateRenderer Error: %s\n", SDL_GetError());
}
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
SDL_Rect rect;
rect.x = 20;
rect.y = 20;
rect.w = 10;
rect.h = 10;
SDL_SetRenderDrawColor(ren, 0, 0, 255, 255);
SDL_RenderFillRect(ren, &rect);
filledCircleColor(ren, 50, 50, 50, 0xFF0000FF);
SDL_RenderPresent(ren);
while(1) { ; }
}
这是我的Makefile:
test:
make build;
make run;
build:
gcc -std=c99 -c sdlTest.c;
gcc sdlTest.o -o sdlTest -I/usr/include/SDL2 -lSDL2;
run:
./sdlTest;
clean:
-rm sdlTest;
-rm *.o;
-rm *~
我在Linux机器上。谢谢你的帮助!
答案 0 :(得分:2)
您需要通过添加SDL2_gfx
选项告诉链接器您正在使用-lSDL2_gfx
扩展名:
gcc sdlTest.o -o sdlTest -lSDL2 -lSDL2_gfx;
请注意-I
标志在这里是多余的,因为您处于联动阶段。