I am trying to get the header file SDL2/SDL.h to work in C and have the following code:
#include <stdio.h>
#include <SDL2/SDL.h>
#define WIDTH 800
#define HEIGHT 600
#define DELAY 1000
int main (int argc, char **argv)
{
SDL_Window *window = NULL;
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "SDL failed to initialise: %s\n", SDL_GetError());
return 1;
}
window = SDL_CreateWindow("SDL Example", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, WIDTH,
HEIGHT, 0);
if (window == NULL) {
fprintf(stderr, "SDL window failed to initialise: %s\n", SDL_GetError());
return 1;
}
SDL_Delay(DELAY);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Note that I have not written this code and I do not understand it 100%. I just wanted something that would work in order to present my question. Anyhow, when I use command prompt to run the program, I enter the following:
gcc -std=c11 main.c -I{Path to SDL2\include} -L{Path to SDL2\lib} -Wall -lmingw32 - lSDL2main -lSDL2 -o main
No errors here, and after I enter:
main.exe
This works just fine, but when I enter the same input as above in Visual Studio Code's terminal there are some problems I wonder about.
Firstly, I need to enter:
.\main.exe
instead of:
main.exe
Why?
Secondly, every time I compile this code I need to enter:
gcc -std=c11 main.c -I{Path to SDL2\include} -L{Path to SDL2\lib} -Wall -lmingw32 - lSDL2main -lSDL2 -o main
(Note that the brackets are not included in the actual code)
However, I am used to only enter:
gcc main.c
followed by:
.\a
But when I enter the former input, I get this error:
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x15): undefined reference
to `SDL_Init'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x1e): undefined reference
to `SDL_GetError'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x78): undefined reference
to `SDL_CreateWindow'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0x86): undefined reference
to `SDL_GetError'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0xb5): undefined reference
to `SDL_Delay'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0xc0): undefined reference
to `SDL_DestroyWindow'
C:\Users\ALEXAN~1\AppData\Local\Temp\cck2ylh0.o:main.c:(.text+0xc5): undefined reference
to `SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):
(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
If I had to enter the long input every time I compiled it would be a nightmare! How can avoid this and keep my familiar habit? I use Windows with a 32-bit MinGW compiler on Visual Studio Code (although I have a 64-bit processor).