BindBC / bindbc-sdl

Static & dynamic D bindings to SDL and the SDL_* libraries, compatible with BetterC, @nogc, and nothrow.
Boost Software License 1.0
86 stars 24 forks source link

[dub] [windows] undefined symbol when compiled statically #67

Closed VadimkaG closed 1 month ago

VadimkaG commented 1 month ago

dub file:

{
    "dependencies": {
        "bindbc-sdl": "~>1.4.8"
    },
    "subConfigurations": {
        "bindbc-sdl": "staticBC",
    },
    "versions": [
        "SDL_2_26",
        "SDL_Image_2_6",
        "SDL_TTF_2_20"
    ],
    "libs": [
        "SDL2",
        "SDL2_image",
        "SDL2_ttf"
    ]
}

It works great on Linux On windows it requires SDL.lib I downloaded SDL2-devel-2.26.5-VC.zip (from libsdl-org/SDL github) and unpacked .lib files into the project directory (For Image and TTF I also downloaded from github)

When compiling I get the following errors: lld-link: error: undefined symbol: _D6bindbc6common7codegen6FnBind9__xtoHashFNbNeKxSQBvQBrQBnQBiZm lld-link: error: undefined symbol: _D6bindbc6common7codegen6FnBind11__xopEqualsMxFKxSQBwQBsQBoQBjZb

DMD64 D Compiler v2.105.0-dirty DUB version 1.34.0, built on Aug 1 2023

ichordev commented 1 month ago

On windows it requires SDL.lib I downloaded SDL2-devel-2.26.5-VC.zip (from libsdl-org/SDL github) and unpacked .lib files into the project directory (For Image and TTF I also downloaded from github)

I checked for myself, it's named SDL2.lib, which any linker should be able to find with -lSDL2 (or equivalent) if it's installed in the right place.

When compiling I get the following errors: lld-link: error: undefined symbol: _D6bindbc6common7codegen6FnBind9__xtoHashFNbNeKxSQBvQBrQBnQBiZm lld-link: error: undefined symbol: _D6bindbc6common7codegen6FnBind11__xopEqualsMxFKxSQBwQBsQBoQBjZb

DMD64 D Compiler v2.105.0-dirty DUB version 1.34.0, built on Aug 1 2023

Seems that the linker is failing to find the compiler-generated methods in FnBind that allow it to be a key in a hash map. Perhaps those methods were never generated by the compiler because they are only used in CTFE? I suspect this is some kind of compiler frontend bug. Please try the latest version of DMD, and also try LDC2 to see what happens.

VadimkaG commented 1 month ago

Looks like this really is a DMD bug. I tried the latest version of DMD, but I encountered the same problem. I tried to install LDC and it compiled without errors

VadimkaG commented 1 month ago

It seems that I jumped to conclusions. It compiles well, but requires a sdl.dll. What did I do wrong? Why isn't it built into a static application?

ichordev commented 1 month ago

It compiles well, but requires a sdl.dll. What did I do wrong?

You haven't shown any errors where your linker was unable to find symbols from SDL2, so your issue is probably not related to SDL2 at all. Make sure to use dub --force when test re-compiling your project without modifying your code.

Why isn't it built into a static application?

Do you mean why wasn't SDL2 isn't statically linked? (i.e. included in the application binary) Well, a .dll is a shared library, you can't statically link against it; that's what the .lib is for. Most linkers prioritise shared library files over static ones, so when there are shared and static versions of a library, the shared one will be used. Please note that SDL is intended to be dynamically linked, because it allows users to update to newer versions of the library (with the latest bug-fixes) without you having to update your software. And there is not really a tangible benefit to static linking over dynamic linking. If you insist on statically linking however, you will have to also link against all of SDL2's dependencies (as static libraries). These dependencies will be different for different platforms, but you can usually just look up each symbol the linker fails to find and figure out what library it belongs to. That said, I do not recommend doing this for SDL2 at all.

VadimkaG commented 1 month ago

Do you mean why wasn't SDL2 isn't statically linked?

yes thanks for the explanations and help.