I'm trying to use SDL2 to display 2D graphics for a simulation. I'm trying out simple SDL2 functions, and I'm getting something called a "linker error"?
Here's my SDL2 test code that should just open up a window for 3 seconds, according to a YouTube tutorial:
#include <SDL2/SDL.h>
#include <iostream>
#include <stdio.h>
int main()
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
}
And here's the error message I get whenever I try to run it:
[Running] cd "/Users/gabriel/Desktop/ideal gas simulation/VS Code/" && g++ SDLploxwork.cpp -o SDLploxwork && "/Users/gabriel/Desktop/ideal gas simulation/VS Code/"SDLploxwork
Undefined symbols for architecture x86_64:
"_SDL_CreateRenderer", referenced from:
_main in SDLploxwork-fa78ca.o
"_SDL_CreateWindow", referenced from:
_main in SDLploxwork-fa78ca.o
"_SDL_Delay", referenced from:
_main in SDLploxwork-fa78ca.o
"_SDL_Init", referenced from:
_main in SDLploxwork-fa78ca.o
"_SDL_RenderPresent", referenced from:
_main in SDLploxwork-fa78ca.o
"_SDL_SetRenderDrawColor", referenced from:
_main in SDLploxwork-fa78ca.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Done] exited with code=1 in 1.037 seconds
The most common fix I've seen online involves poking at a VS Code settings file called tasks.json, specifically the list of "args", so here's that... These are the default settings except for the "-framework", "SDL2"
lines suggested by @HolyBlackCat.
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"-framework",
"SDL2"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
If I write a hello world program with #include <SDL2/SDL.h>
at the top, there's no trouble. It seems like the thing knows where the library is, it just apparently doesn't know where its functions are. And neither do I.
I'm using VS Code 1.56.2 on Mac OS 10.13.6 to write C++98, trying to use SDL 2.0.16 "development library" from here with clang-1000.10.44.4. If there's any more settings text files or whatever I can post to help find the problem, I'll do it.
How can I get this library to work? Does anyone have any ideas?