0

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
genpfault
  • 51,148
  • 11
  • 85
  • 139
emir
  • 1
  • 4
  • are your SDL libraries built with the same compiler as you're compiling your application with? – Alan Birtles Jul 07 '23 at 12:00
  • @AlanBirtles yes, I mean I quess so. I downloaded Mingw and I added mingw32-base and mingw-gcc-g++. But I don't use it. I just click run in VS Code that I got with code runner extansion. – emir Jul 07 '23 at 12:08
  • @AlanBirtles like I mean I don't use MSYS2 MSYS that came with mingw. – emir Jul 07 '23 at 12:09
  • 1
    where did you get the sdl libraries from? – Alan Birtles Jul 07 '23 at 12:12
  • @AlanBirtles I downloaded it from https://github.com/libsdl-org/SDL/releases/tag/release-2.28.1 this url and I downloaded SDL2-devel-2.28.1-mingw.tar.gz – emir Jul 07 '23 at 12:15
  • Sounds like it's much faster to install any mainstream Linux distribution, with the current version of gcc, and SDL, already correctly configured, out the box, and ready to use; instead of tinkering here and there trying to figure out why separately installed components are broken and don't work together. – Sam Varshavchik Jul 07 '23 at 12:37
  • @SamVarshavchik lmfao – emir Jul 07 '23 at 12:44
  • Please edit your question and include the actual command line that was invoked, as printed by make, that gave these errors, not just the errors. – MadScientist Jul 07 '23 at 13:09
  • 1
    ***I mean I don't use MSYS2 MSYS that came with mingw.*** Your task would be much simpler if you did use the [VSCode recommended MinGW](https://code.visualstudio.com/docs/cpp/config-mingw) installation method. Also you could use `pacman` inside of msys2 and install SDL2 that is compatible with the MinGW you install. – drescherjm Jul 07 '23 at 13:16
  • OP, you have two options: either install SDL2 from MSYS2 (which is recommended), as described [here](https://stackoverflow.com/q/30069830/2752075). Or follow the troubleshooting guide [here](https://stackoverflow.com/q/64396979/2752075) if you prefer the separately downloaded SDL2 for whatever reason. – HolyBlackCat Jul 07 '23 at 17:26
  • @SamVarshavchik The only issue with Windows is the number of alternatives to choose from. MSYS2 would let you install this library with a single command too, exactly like your favorite Linux distribution. And so would vcpkg, if you can tolerate MSVC. And while I too think that Linux is a good system, the way to promote it is with its actual advantages, not with "I don't know how to develop on Windows, therefore it must be harder than what I know". – HolyBlackCat Jul 07 '23 at 17:40
  • I can think of plenty more issues with MS-Win, @HolyBlackCat; but where are all these questions about building and linking stuff on Ubuntu or Debian or Fedora? Even Arch? – Sam Varshavchik Jul 07 '23 at 17:51
  • @SamVarshavchik They are simply less popular, hence less questions. Also they tend to have a single package manager, so a single way of doing things without having to choose between alternatives. And again, building on e.g. MSYS2 is exactly the same as building on Linux (same commands, etc). The only difference is having to *know* that MSYS2 is a thing. – HolyBlackCat Jul 07 '23 at 18:02

0 Answers0