I recently wanted to try out SDL2 using C and I followed this tutorial Once everything was done I ran the code but I came across an error I did search the web but I did not find any clear answer
.\main.c:1:22: fatal error: SDL2/SDL.h: No such file or directory
#include <SDL2/SDL.h>
^
compilation terminated.
This was my configuration for
c_cpp_properties.json
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
//Pay attention to change to your own MinGW directory
"C:\\MinGW\\x86_64-w64-mingw32\\include\\**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "g++.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
//Pay attention to change to your own MinGW directory
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++.exe build active file"
}
]
}
tasks.json
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "g++.exe build active file",
//Pay attention to change to your own MinGW directory
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-lmingw32",
"-lSDL2main",
"-lSDL2",
"-mwindows"
],
"options": {
//Pay attention to change to your own MinGW directory
"cwd": "C:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
I have also included the SDL2.dll in the root directory I have copied the 64bit folder of the SDL2
Thanks.
Edit: This is the code that was running in the main.c file
#include <SDL2/SDL.h>
//#include <stdio.h>
//#include <stdlib.h>
SDL_Window *window = NULL;
SDL_Surface *surface = NULL;
//The parameters in the main function cannot be omitted, or an error will be reported
int main(int arg, char *argv[])
{
window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
if (!window)
return -1;
surface = SDL_GetWindowSurface(window);
if (!surface)
return -2;
SDL_Rect rec;
rec.x = 700;
rec.y = 10,
rec.w = 20;
rec.h = 20;
while (1)
{
SDL_FillRect(surface, &rec, SDL_MapRGB(surface->format, 180, 10, 140));
rec.x += 6;
rec.y += 2;
rec.x = rec.x > 800 ? 0 : rec.x;
rec.y = rec.y > 600 ? 0 : rec.y;
SDL_FillRect(surface, &rec, SDL_MapRGB(surface->format, 10, 200, 120));
SDL_UpdateWindowSurface(window);
SDL_Delay((1.0 / 60) * 1000);
}
SDL_FreeSurface(surface);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}