I've been trying to learn SDL2 with the SDL2_gfx library, but I'm having difficulties getting an example program to compile. When I run:
g++ -c -Wall main.cpp -o main.o
g++ main.o -L/usr/local/lib -lSDL2 -framework SDL2 -o main
Undefined symbols for architecture x86_64:
"_boxColor", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
I've tried a number of different things as well, such as using sdl2-config --libs --cflags
, but this gives the same error. When just using SDL2 functions, it compiles fine, it's the SDL2_gfx functions that are the issue. I'm relatively new to C++, but when I accidentally made a syntax error with the boxColor
function, the compiler caught it, so I think it's being included properly in my program. Any help would be appreciated.
Here's my code as well:
#include <SDL2/SDL.h>
#include <SDL2/SDL2_gfxPrimitives.h>
#define WIDTH 640
#define HEIGHT 480
int main(int argc, char* argv[])
{
if (SDL_Init(SDL_INIT_VIDEO))
{
printf ("SDL_Init Error: %s", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow("SDL2_gfx test", 100, 100, WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
if (window == NULL)
{
printf ("SDL_CreateWindow Error: %s", SDL_GetError());
SDL_Quit();
return 2;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL)
{
SDL_DestroyWindow(window);
printf ("SDL_CreateRenderer Error: %s", SDL_GetError());
SDL_Quit();
return 3;
}
SDL_Event e;
int quit = 0;
while (!quit)
{
if (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
quit = 1;
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0xFF, 0xFF);
boxColor(renderer, WIDTH / 4, HEIGHT / 4, 3 * WIDTH / 4, 3* HEIGHT / 4, 0xFF0000FF);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}