I coded some block of code that I display down below. I am trying to use SDL2 for game development with C++ but I get some undefined reference to SDL errors. I am going to display those errors down below. First, I downloaded SDL2 and I copied the folder to my folder in VS Code. I included it in my C++ code. But even if the full folder and all of the files that comes with SDL2 is in my projects folder, I get the errors.
main.cpp
#include <iostream>
#define SDL_MAIN_HANDLED
#include "SDL2/i686-w64-mingw32/include/SDL.h"
const int WIDTH = 800, HEIGHT = 600;
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window *window = SDL_CreateWindow( "Hello SDL WORLD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );
if ( NULL == window )
{
std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
return 1;
}
SDL_Event windowEvent;
while ( true )
{
if ( SDL_PollEvent( &windowEvent ) )
{
if ( SDL_QUIT == windowEvent.type )
{ break; }
}
}
SDL_DestroyWindow( window );
SDL_Quit( );
return EXIT_SUCCESS;
}
Makefile:
CC := g++
CFLAGS := -std=c++17 -I"C:/Users/ASUS/.vscode/gamedevelopment/SDL2/i686-w64-mingw32/include"
LFLAGS := -L"C:/Users/ASUS/.vscode/gamedevelopment/SDL2/i686-w64-mingw32/lib" -lSDL2 -lSDL2main
all: main
main: main.cpp
$(CC) $(CFLAGS) $< $(LFLAGS) -o $@
.PHONY: clean
clean:
rm -f main
When I run the code with code runner, I see the errors in the terminal of my VS Code.
Errors:
C:\Users\ASUS\AppData\Local\Temp\ccyIfXkF.o:main.cpp:(.text+0x1f): undefined reference to `SDL_Init'
C:\Users\ASUS\AppData\Local\Temp\ccyIfXkF.o:main.cpp:(.text+0x53): undefined reference to `SDL_CreateWindow'
C:\Users\ASUS\AppData\Local\Temp\ccyIfXkF.o:main.cpp:(.text+0x61): undefined reference to `SDL_GetError'
C:\Users\ASUS\AppData\Local\Temp\ccyIfXkF.o:main.cpp:(.text+0xa6): undefined reference to `SDL_PollEvent'
C:\Users\ASUS\AppData\Local\Temp\ccyIfXkF.o:main.cpp:(.text+0xc7): undefined reference to `SDL_DestroyWindow'
C:\Users\ASUS\AppData\Local\Temp\ccyIfXkF.o:main.cpp:(.text+0xcc): undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
I changed Makefile hundreds of times. Here are some that I tried:
g++ -std=c++17 main.cpp -I"src/include" -L"path/to/sdl2/library" -lSDL2 -lSDL2main -o main
CXX = g++
CXXFLAGS = -I src/include
LDFLAGS = -L src/lib
LDLIBS = -lmingw32 -lSDL2main -lSDL2
ifeq ($(OS),Windows_NT)
LDLIBS += -mwindows
endif
all: main
main: main.cpp
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o main main.cpp $(LDLIBS)
all:
g++ -I src/include -L src/lib -o main main.cpp -lmingw32 -lSDL2main -lSDL2