#SDL2 - problems with including library

146 messages Β· Page 1 of 1 (latest)

near bridgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

jovial crow
#

This is my code:

#include <iostream>
#include <SDL2/SDL.h>




const int WIDTH = 800, HEIGHT = 600;    // Window Dimensions




int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    // Create Window
    SDL_Window *window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALWAYS_ON_TOP);

    // Catch if window is null
    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;
}
#

This is where the compile args are (tasks.json)

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C://w64devkit//bin//g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}//${fileBasenameNoExtension}.exe",

                "-m32",

                "-ISDL2-2.28.5/i686-w64-mingw32/include",

                "-LSDL2-2.28.5/i686-w64-mingw32/lib",
                
                "-lSDL2",
                "-lSDL2main"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C://w64devkit//bin//g++.exe"
        }
    ]
}
#
"args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}//${fileBasenameNoExtension}.exe",

                "-m32",

                "-ISDL2-2.28.5/i686-w64-mingw32/include",

                "-LSDL2-2.28.5/i686-w64-mingw32/lib",
                
                "-lSDL2",
                "-lSDL2main"
            ],
```Potentially main focus
#

Most importantly, here's the whole source code... because I don't know what I'm doing haha

#

I've been trying and trying for a few hours

#

and somehow, it's already 4:30 am

#

I'm going to sleep, and hope someone magical enough with that keyboard, has come to offer me their help, and has pointed out where I've gone wrong, even though though I haven't paid them anything

#

_ _

#

I'm just trying to use my code with this library : SDL2

#

that's all that's going on in there

stoic monolith
#

first thing I've noticed is that you are linking 32bit sdl, is there any reason for that? Might not be what's causing the issue but still would try linking the 64bit one

#

But looking at your errors it might actually be the problem

stoic monolith
#

@jovial crow I'm pretty sure this is your problem tho, the compiler is going crazy because of it

jovial crow
#

I used -m32 in the g++ args to make sure it's compiling for 32 bit

#

and I used the 32 bit build for the library

#

because I won't need more than 4GB of ram, and it's more compatiable with old computers as well as new computers

#

I redid it with 64 bit

#
Starting build...
C://w64devkit//bin//g++.exe -fdiagnostics-color=always -g C:\Users\jason\Desktop\SimpleMusicPlayer2\main.cpp -o C:\Users\jason\Desktop\SimpleMusicPlayer2//main.exe -ISDL2-2.28.5/x86_64-w64-mingw32/include -LSDL2-2.28.5/x86_64-w64-mingw32/lib -lSDL2 -lSDL2main
C:\w64devkit\bin/ld.exe: C:/w64devkit/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text.startup+0xb1): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
#

but now I have this error

#

and SDL2 still isn't found

#
#include <iostream>
#include <SDL2/SDL.h>




const int WIDTH = 800, HEIGHT = 600;    // Window Dimensions




int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    // Create Window
    SDL_Window *window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALWAYS_ON_TOP);

    // Catch if window is null
    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;
}

code seems fine, and the include path should have no issues

#

I don't know why it is doing this

#

I did everything right

#

so now I have no errors

#

I switched main for WinMain

#

but now this comes up

#

it's an error that's not detected in vscode

#

THIS IS WEIRD

#

for SDL2.dll to be found, I have to copy it into the same dir as the main.cpp

#

it shouldn't need to do that

#

I thought I could just leave the dll in it's library, but I guess not?

#

was I supposed to put dll's in the same dir as the main executable?

#

I'm new to importing stuff

hexed shell
#

You need to learn how DLLs are searched.

stoic monolith
#

DLLs are searched in every directory that is in PATH + the root directory of the binary

stoic monolith
hexed shell
#

The correct way is to write SDL_main.

stoic monolith
#

If you call the inits you don't really have to

jovial crow
#

I used SDLMain

#
Starting build...
C://w64devkit//bin//g++.exe -fdiagnostics-color=always -g C:\Users\jason\Desktop\SimpleMusicPlayer2\main.cpp -o C:\Users\jason\Desktop\SimpleMusicPlayer2//main.exe -ISDL2-2.28.5/x86_64-w64-mingw32/include -LSDL2-2.28.5/x86_64-w64-mingw32/lib -lSDL2 -lSDL2main -lmingw32
C:\w64devkit\bin/ld.exe: C:/w64devkit/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text.startup+0xb1): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).
#

I don't know why it's doing this

#

_ _

#
#include <iostream>
#include <Windows.h>
#include <SDL2/SDL.h>




const int WIDTH = 800, HEIGHT = 600;    // Window Dimensions




int SDLMain( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALWAYS_ON_TOP);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    // Call GetConsoleWindow to retrieve the console window handle
    HWND consoleWindow = GetConsoleWindow();
    if (consoleWindow == NULL)
    {
        // Hide console window
        ShowWindow(consoleWindow, SW_HIDE);
    }

    SDL_Event windowEvent;

    while ( true )
    {
        if (SDL_PollEvent( &windowEvent ))
        {
            if (SDL_QUIT == windowEvent.type)
            { break; }
        }

        const Uint8 *state = SDL_GetKeyboardState(NULL);

        // Check for Ctrl+Shift+C
        if (state[SDL_SCANCODE_C] && state[SDL_SCANCODE_LCTRL] && state[SDL_SCANCODE_LSHIFT]) {
            printf("Ctrl+Shift+C pressed!\n");
        }

        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }


    SDL_DestroyWindow( window );
    SDL_DestroyRenderer( renderer );
    SDL_Quit();

    return EXIT_SUCCESS;
}
jovial crow
hexed shell
jovial crow
#

oooooh

#

thank you

#
#include <iostream>
#include <Windows.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_main.h>




const int WIDTH = 800, HEIGHT = 600;    // Window Dimensions




int SDLMain( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALWAYS_ON_TOP);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    // Call GetConsoleWindow to retrieve the console window handle
    HWND consoleWindow = GetConsoleWindow();
    if (consoleWindow == NULL)
    {
        // Hide console window
        ShowWindow(consoleWindow, SW_HIDE);
    }

    SDL_Event windowEvent;

    while ( true )
    {
        if (SDL_PollEvent( &windowEvent ))
        {
            if (SDL_QUIT == windowEvent.type)
            { break; }
        }

        const Uint8 *state = SDL_GetKeyboardState(NULL);

        // Check for Ctrl+Shift+C
        if (state[SDL_SCANCODE_C] && state[SDL_SCANCODE_LCTRL] && state[SDL_SCANCODE_LSHIFT]) {
            printf("Ctrl+Shift+C pressed!\n");
        }

        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }


    SDL_DestroyWindow( window );
    SDL_DestroyRenderer( renderer );
    SDL_Quit();

    return EXIT_SUCCESS;
}
#

same error

#
Starting build...
C://w64devkit//bin//g++.exe -fdiagnostics-color=always -g C:\Users\jason\Desktop\SimpleMusicPlayer2\main.cpp -o C:\Users\jason\Desktop\SimpleMusicPlayer2//main.exe -ISDL2-2.28.5/x86_64-w64-mingw32/include -LSDL2-2.28.5/x86_64-w64-mingw32/lib -lSDL2 -lSDL2main -lmingw32
C:\w64devkit\bin/ld.exe: C:/w64devkit/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text.startup+0xb1): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).
gleaming swallow
#

Are you compiling with -mwindows?

stoic monolith
# hexed shell The correct way is to write `SDL_main`.
You should be using main() instead of WinMain() even though you are creating a Windows application, because SDL provides a version of WinMain() which performs some SDL initialization before calling your main code.

If for some reason you need to use WinMain(), take a look at the SDL source code in src/main/win32/SDL_main.c to see what kind of initialization you need to do in your WinMain() function so that SDL works properly.
#

from the SDL Windows FAQ πŸ™‚

drifting salmon
#

If you want an advice use a project manager like cmake or xmake

#

It will be easier to setup a project

hexed shell
gleaming swallow
jovial crow
#

So I should use main(), as well as #define main SDL_main?

#
#include <iostream>
#include <Windows.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_main.h>




const int WIDTH = 800, HEIGHT = 600;    // Window Dimensions




int main( int argc, char *argv[] )
{
    SDL_Init( SDL_INIT_EVERYTHING );

    SDL_Window *window = SDL_CreateWindow("Test Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALWAYS_ON_TOP);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    // Call GetConsoleWindow to retrieve the console window handle
    HWND consoleWindow = GetConsoleWindow();
    if (consoleWindow == NULL)
    {
        // Hide console window
        ShowWindow(consoleWindow, SW_HIDE);
    }

    SDL_Event windowEvent;

    while ( true )
    {
        if (SDL_PollEvent( &windowEvent ))
        {
            if (SDL_QUIT == windowEvent.type)
            { break; }
        }

        const Uint8 *state = SDL_GetKeyboardState(NULL);

        // Check for Ctrl+Shift+C
        if (state[SDL_SCANCODE_C] && state[SDL_SCANCODE_LCTRL] && state[SDL_SCANCODE_LSHIFT]) {
            printf("Ctrl+Shift+C pressed!\n");
        }

        SDL_RenderClear(renderer);
        SDL_RenderPresent(renderer);
    }


    SDL_DestroyWindow( window );
    SDL_DestroyRenderer( renderer );
    SDL_Quit();

    return EXIT_SUCCESS;
}
#
Starting build...
C://w64devkit//bin//g++.exe -fdiagnostics-color=always -g C:\Users\jason\Desktop\SimpleMusicPlayer2\main.cpp -o C:\Users\jason\Desktop\SimpleMusicPlayer2//main.exe -ISDL2-2.28.5/x86_64-w64-mingw32/include -LSDL2-2.28.5/x86_64-w64-mingw32/lib -lSDL2 -lSDL2main -lmingw32
C:\w64devkit\bin/ld.exe: C:/w64devkit/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text.startup+0xb1): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).
#

I used main() this time like everyone said, but it looks like I got the same error

#

this error makes no sense

#

I know SDL uses WinMain, but why am I getting the error

#

I used main for the entrypoint this time

jovial crow
jovial crow
#

how does it help, everyone keeps suggesting it

drifting salmon
#

Not only. It’s a very powerful tool which allow you to setup a project in a simple file. For beginners I recomend xmake which is newer but easiest than cmake and use lua which is easier.
Xmake comes with xrepo which download automatically libs like sdl2 for you and setting up it

jovial crow
#

what if I only have one dependecy to add

#

should I use it on a small project

#

I'll use it

#

but I wanna get the code working first

jovial crow
#

#define SDL_MAIN_HANDLED I added this before including SDL.h

#

and it works

#

why is that? Why do I need this?

#

Is this recommended, is it used normally?

hexed shell
#

It's not recommended. You are recommended to write main and build your project correctly.

drifting salmon
jovial crow
drifting salmon
#

If you have questions about sdl2 you can @ me I used a lot this library some years ago

hexed shell
#

I said this way back:

You need to also supply the SDL2Main library.

jovial crow
jovial crow
#

I can do it again

#
Starting build...
C://w64devkit//bin//g++.exe -fdiagnostics-color=always -g C:\Users\jason\Desktop\SimpleMusicPlayer2\main.cpp -o C:\Users\jason\Desktop\SimpleMusicPlayer2//main.exe -ISDL2-2.28.5/x86_64-w64-mingw32/include -LSDL2-2.28.5/x86_64-w64-mingw32/lib -lSDL2 -lSDL2main -lmingw32
C:\w64devkit\bin/ld.exe: C:/w64devkit/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crtexewin.o):crtexewin.c:(.text.startup+0xb1): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
#
#include <iostream>
#include <Windows.h>
#include <stdio.h>


#include <SDL2/SDL_main.h>
#include <SDL2/SDL.h>
#

without: #define SDL_MAIN_HANDLED

#

and with it ^

#

it builds

#

no errors or warnings

#

but I don't think it's good

#

#define SDL_MAIN_HANDLED feels like a workaround

hexed shell
#

Try putting -lmingw32 to before -lSDL2.

jovial crow
#

ok

#

nope

#

same error

hexed shell
#

Show it to be sure.

jovial crow
#

ok

#
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "C://w64devkit//bin//g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}//${fileBasenameNoExtension}.exe",

                "-ISDL2-2.28.5/x86_64-w64-mingw32/include",

                "-LSDL2-2.28.5/x86_64-w64-mingw32/lib",
                
                "-lmingw32",
                "-lSDL2",
                "-lSDL2main"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "compiler: C://w64devkit//bin//g++.exe"
        }
    ]
}
#

look for "args"

hexed shell
#

I mean the error message.

jovial crow
#

oh

#
Starting build...
C://w64devkit//bin//g++.exe -fdiagnostics-color=always -g C:\Users\jason\Desktop\SimpleMusicPlayer2\main.cpp -o C:\Users\jason\Desktop\SimpleMusicPlayer2//main.exe -ISDL2-2.28.5/x86_64-w64-mingw32/include -LSDL2-2.28.5/x86_64-w64-mingw32/lib -lmingw32 -lSDL2 -lSDL2main
C:\w64devkit\bin/ld.exe: SDL2-2.28.5/x86_64-w64-mingw32/lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:66: undefined reference to `SDL_strlen'
C:\w64devkit\bin/ld.exe: /Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:71: undefined reference to `SDL_memcpy'
C:\w64devkit\bin/ld.exe: /Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:72: undefined reference to `SDL_free'
C:\w64devkit\bin/ld.exe: /Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:62: undefined reference to `SDL_wcslen'
C:\w64devkit\bin/ld.exe: /Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:62: undefined reference to `SDL_iconv_string'
C:\w64devkit\bin/ld.exe: SDL2-2.28.5/x86_64-w64-mingw32/lib/libSDL2main.a(SDL_windows_main.o): in function `OutOfMemory':
/Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:25: undefined reference to `SDL_ShowSimpleMessageBox'
C:\w64devkit\bin/ld.exe: SDL2-2.28.5/x86_64-w64-mingw32/lib/libSDL2main.a(SDL_windows_main.o): in function `main_getcmdline':
/Users/valve/release/SDL2/SDL2-2.28.5-source/foo-x64/../src/main/windows/SDL_windows_main.c:77: undefined reference to `SDL_SetMainReady'
collect2.exe: error: ld returned 1 exit status

Build finished with error(s).
hexed shell
#

Not the same error isn't it?

jovial crow
#

well... it is bigger

#

my bad

hexed shell
#

Move -lSDL2 to after -lSDL2main.

jovial crow
#

ok

#

it builds now

#

thanks

#

how did you know it was the order?

#

the error message doesn't say anything about it

hexed shell
#

Indeed, quite involved knowledge.

#

The order is if A needs things in B then A comes before B.

jovial crow
#

oh

#

that makes sense

#

I never thought about that

#

main uses mingw32, and sdl2 uses main

hexed shell
#

Not really, mingw32 needs main, which SDL2main provides.

#

Then SDL2main calls things in SDL2.

jovial crow
#

but isn't main loaded before SDL2

#
"-lmingw32",
"-lSDL2main",
"-lSDL2"
hexed shell
#

Yes.

#

That doesn't contradict anything I said.

jovial crow
#

main calls from sld2 before it's loaded

hexed shell
jovial crow
#

oh

#

I get it now

#

other way around-kind of

#

_ _

#

thanks for helping me! :D