Windows10下MinGW GCC编译C代码报错:SDL2头文件未找到求助
Hey there, let's get this compilation error sorted out—this is a common hiccup when setting up SDL2 with MinGW, and we can fix it in two straightforward steps:
1. Correct the #include Statement
First off, your include line is misformatted. SDL2 doesn't have a top-level header file named SDL2; you need to reference the main SDL2 header file that lives inside the SDL2 directory. Update your code to this:
#include <SDL2/SDL.h> #include <stdio.h> int main() { // Quick tip: Use int main() instead of void main() to follow C standards printf("Testing"); return 0; }
(I also swapped void main() for int main() with a return statement—this avoids undefined behavior and keeps your code compliant with standard C.)
2. Update Your Compilation Command
MinGW GCC doesn't automatically know where SDL2's headers and libraries are stored, so you need to explicitly point it to those locations:
-I<path-to-SDL2-include>: Tells the compiler where to find SDL2's header files-L<path-to-SDL2-lib>: Tells the linker where to find SDL2's library files-lSDL2main -lSDL2: Links against the SDL2 main library (handles Windows entry points) and the core SDL2 library
Assuming you installed SDL2 to C:\SDL2, your full compile command would look like this:
gcc test.c -o test -IC:\SDL2\include -LC:\SDL2\lib -lSDL2main -lSDL2
If you're using a 64-bit version of MinGW, make sure you downloaded the 64-bit SDL2 binaries, and adjust the library path to match (e.g., C:\SDL2\lib\x86_64-w64-mingw32 instead of C:\SDL2\lib).
Quick Sanity Check
Double-check that your SDL2 installation has:
- The
SDL2/SDL.hfile inside theincludedirectory libSDL2main.aandlibSDL2.a(or their DLL equivalents) inside thelibdirectory
Once you make these changes, your code should compile without issues!
内容的提问来源于stack exchange,提问作者MauroDev

