Thread overview
Using bindbc-sdl with D
Mar 12, 2023
idsize
Mar 12, 2023
ryuukk_
Mar 12, 2023
idsize
Mar 12, 2023
ryuukk_
Mar 12, 2023
idsize
March 12, 2023

I started learning D a few weeks ago and am enjoying it so far. I would like to use SDL with D and found bindbc-sdl, but I cannot figure out how to make it work.

From my understanding, I'll need to use 'dub fetch bindbc-sdl' to download it, and then run 'dub build bindbc-sdl' to build it, before I can use it. Fetching it works, but it always fails to build. I get this error: 'failed launching cl.exe /P /Zc:preprocessor /PD /nologo ... '.

Additionally, a more general question about bindbc, is it possible to skip using dub altogether? Like would placing the bindbc files inside of 'C:\D\dmd2\src\druntime\import' work directly?

March 12, 2023
On 12/03/2023 3:12 PM, idsize wrote:
> I started learning D a few weeks ago and am enjoying it so far. I would like to use SDL with D and found bindbc-sdl, but I cannot figure out how to make it work.

Welcome!

>  From my understanding, I'll need to use 'dub fetch bindbc-sdl' to download it, and then run 'dub build bindbc-sdl' to build it, before I can use it. Fetching it works, but it always fails to build. I get this error: 'failed launching cl.exe /P /Zc:preprocessor /PD /nologo ... '.

You don't need to do that, add it as a dependency in your dub package and dub will do the rest.

But ugh that error looks weird. That would imply its trying to run the C preprocessor for ImportC. That does not sound right at all, I don't see any C headers/code in bindbc-sdl repo (not that it needs it). Gonna need to see the entire error log for that to know whats going on (and possibly the verbose output).

> Additionally, a more general question about bindbc, is it possible to skip using dub altogether? Like would placing the bindbc files inside of 'C:\D\dmd2\src\druntime\import' work directly?

Yes, but not that way. Druntime/phobos are both distributed compiled, the import directories tell the compiler what has been compiled in and allow access to templates defined by the library (which you then compile).

If you add the appropriate versions and copy the files to your project directory (including bindbc-loader) that should work however as long as you compile it in.
March 12, 2023

On Sunday, 12 March 2023 at 02:12:45 UTC, idsize wrote:

>

I started learning D a few weeks ago and am enjoying it so far. I would like to use SDL with D and found bindbc-sdl, but I cannot figure out how to make it work.

From my understanding, I'll need to use 'dub fetch bindbc-sdl' to download it, and then run 'dub build bindbc-sdl' to build it, before I can use it. Fetching it works, but it always fails to build. I get this error: 'failed launching cl.exe /P /Zc:preprocessor /PD /nologo ... '.

Additionally, a more general question about bindbc, is it possible to skip using dub altogether? Like would placing the bindbc files inside of 'C:\D\dmd2\src\druntime\import' work directly?

Hello, and welcome!

Looks like the new version of that library is broken, i am having the same issue

Reverting to previous (1.2.4) one is working (delete the dub.selection.json):

Download SDL: https://github.com/libsdl-org/SDL/releases/tag/release-2.26.4

And put the dll in your project folder

{
	"name": "hellosdl",
	"dependencies": {
		"bindbc-sdl": "1.2.4",
	},
	"versions": [
		"SDL_2_26"
	],
}

import bindbc.sdl;
import core.stdc.stdio;

void main()
{

    // here we load the dll
    auto ret = loadSDL();

    // here handle cases where it failed to load the dll
    if (ret != sdlSupport)
    {
        if (ret == SDLSupport.noLibrary)
        {
            // dll not found
        }
        else if (ret == SDLSupport.badLibrary)
        {
            // wrong DLL, perhaps 32bit vs 64bit?
        }
        return;
    }

    // all good, let's use SDL

    SDL_Window* window = null;
    SDL_Surface* screenSurface = null;
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
        return;
    }
    window = SDL_CreateWindow("hello_sdl2", SDL_WINDOWPOS_UNDEFINED,
            SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
    if (window == null)
    {
        fprintf(stderr, "could not create window: %s\n", SDL_GetError());
        return;
    }

    screenSurface = SDL_GetWindowSurface(window);
    bool running = true;
    while (running)
    {
        SDL_Event ev;
        while (SDL_PollEvent(&ev))
        {
            switch (ev.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            default:
                break;
            }
        }

        SDL_FillRect(screenSurface, null, SDL_MapRGB(screenSurface.format, 0x00, 0x00, 0xFF));
        SDL_UpdateWindowSurface(window);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
}

I filled an issue on the tracker: https://github.com/BindBC/bindbc-sdl/issues/50

March 12, 2023
On 12/03/2023 3:24 PM, Richard (Rikki) Andrew Cattermole wrote:
> But ugh that error looks weird. That would imply its trying to run the C preprocessor for ImportC. That does not sound right at all, I don't see any C headers/code in bindbc-sdl repo (not that it needs it). Gonna need to see the entire error log for that to know whats going on (and possibly the verbose output).

Ah huh, there is one!

https://github.com/BindBC/bindbc-sdl/blob/master/source/bindbc/sdl/ctypes.c

That explains it.
March 12, 2023
On Sunday, 12 March 2023 at 02:59:20 UTC, Richard (Rikki) Andrew Cattermole wrote:
> On 12/03/2023 3:24 PM, Richard (Rikki) Andrew Cattermole wrote:
>> But ugh that error looks weird. That would imply its trying to run the C preprocessor for ImportC. That does not sound right at all, I don't see any C headers/code in bindbc-sdl repo (not that it needs it). Gonna need to see the entire error log for that to know whats going on (and possibly the verbose output).
>
> Ah huh, there is one!
>
> https://github.com/BindBC/bindbc-sdl/blob/master/source/bindbc/sdl/ctypes.c
>
> That explains it.

So this is a dub issue?

It is able to find the linker, why can't it find the preprocessor?

March 12, 2023
On 12/03/2023 4:01 PM, ryuukk_ wrote:
> So this is a dub issue?

Nope, dub is doing everything ok, this is for ImportC.

> It is able to find the linker, why can't it find the preprocessor?

Its probably not installed. We don't ship a C toolchain, it uses the system one (MSVC, which you would normally get via Visual Studio).

March 12, 2023

On Sunday, 12 March 2023 at 02:52:27 UTC, ryuukk_ wrote:

>

On Sunday, 12 March 2023 at 02:12:45 UTC, idsize wrote:

>

[...]

Hello, and welcome!

Looks like the new version of that library is broken, i am having the same issue

[...]

Thanks! It compiled and ran your example successfully.

March 12, 2023

On Sunday, 12 March 2023 at 02:24:31 UTC, Richard (Rikki) Andrew Cattermole wrote:

>

If you add the appropriate versions and copy the files to your project directory (including bindbc-loader) that should work however as long as you compile it in.

I've never used a package manager before so the entire process of using one is still strange to me. When using SDL2 with C, I have all of the SDL2 files within my MinGW install. To compile I use 'gcc example.c -lmingw32 -lSDL2 -lSDL2main'. Could I use a method similar to that?

Also here is the full (but not verbose) error log:
'''
        Starting Performing "debug" build using C:\D\dmd2\windows\bin64\dmd.exe for x86_64.
        Building bindbc-sdl 1.3.0: building configuration [staticBC]
    failed launching cl.exe /P /Zc:preprocessor /PD /nologo C:\Users\idoug\AppData\Local\dub\packages\bindbc-sdl-1.3.0\bindbc-sdl\source\bindbc\sdl\ctypes.c /FIC:\D\dmd2\windows\bin64\..\..\src\druntime\import\importc.h /Fictypes.i
    C:\Users\idoug\AppData\Local\dub\packages\bindbc-sdl-1.3.0\bindbc-sdl\source\bindbc\sdl\config.d(127,16): Error: C preprocess command cl.exe failed for file C:\Users\idoug\AppData\Local\dub\packages\bindbc-sdl-1.3.0\bindbc-sdl\source\bindbc\sdl\ctypes.c, exit status 1

Error C:\D\dmd2\windows\bin64\dmd.exe failed with exit code 1.
'''

March 12, 2023
On 12/03/2023 4:27 PM, idsize wrote:
> On Sunday, 12 March 2023 at 02:24:31 UTC, Richard (Rikki) Andrew Cattermole wrote:
>> If you add the appropriate versions and copy the files to your project directory (including bindbc-loader) that should work however as long as you compile it in.
> 
> I've never used a package manager before so the entire process of using one is still strange to me. When using SDL2 with C, I have all of the SDL2 files within my MinGW install. To compile I use 'gcc example.c -lmingw32 -lSDL2 -lSDL2main'. Could I use a method similar to that?

Yeah pretty much the same thing in principle.

Pass the D files to the compiler (including the bindings, dynamic bindings are not like headers, they load the DLL at runtime not during linking like static bindings), tell it the versions needed for the bindings and it should work.

When you do a successful build with the verbose flag it'll tell you what commands were used to build and link it. That should give you some pointers of what is required (although there will be extra flags/versions specified that you probably won't need).