Reputation: 21
I'm going to start doing a game project but when I'm trying to install SDL2 in VSCode. However, when I compile main.cpp there's a problem: fatal error: SDL2/SDL.h: No such file or directory
I tried to follow the instruction on Youtube but this error keep happening. I put the "SDL2.dll" to the same directory of the main.cpp file This my "Makefile" file:
all:
g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2
My c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/src/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++23",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
All of this file are in the same directory as main.cpp
Upvotes: 2
Views: 5386
Reputation: 11
fatal error: <some_header_file.h>: No such file or directory
Is caused when the compiler cant find the header file specified by an #include
statement in your code. In this case it must be because there is no SDL2/SDL.h file in your include path.
To link with a library in c++ you have to both tell the linker where to find an already compiled library file (on windows this is either a .dll or a .lib file), and you have to tell the compiler where to find the location of the libraries header files, for compiling with your own code.
This is because the compiler needs the definitions contained in the SDL header files to ensure your own code gets compiled correctly in such a way that the linker can actually link it with the compiled library later.
According to your question all you did was
I put the "SDL2.dll" to the same directory of the main.cpp
This makes the linker able to find the library, however you also have to make the compiler able to find the SDL header files.
To do this you have to either copy over the include directory of SDL into your own include path (in this case it seems to be in src/include/) or tell the compiler where to find it, which you can do with the -I flag
Upvotes: 1