www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Use bindbc-sdl statically

reply Ishax <isaacbunsen gmail.com> writes:
I am trying to get graphics working in D. All seems well with 
bindbc sdl, but I hit a nitpick: I prefer to be able to create a 
standalone executable when possible. I cannot get bindbc to be 
happy with sdl .a files. It's a small problem, but if there's a 
simple solution, I'm eager to hear it.
Apr 24
parent reply russhy <russhy gmail.com> writes:
On Saturday, 24 April 2021 at 15:58:26 UTC, Ishax wrote:
 I am trying to get graphics working in D. All seems well with 
 bindbc sdl, but I hit a nitpick: I prefer to be able to create 
 a standalone executable when possible. I cannot get bindbc to 
 be happy with sdl .a files. It's a small problem, but if 
 there's a simple solution, I'm eager to hear it.
.a are object files for linux right? for static usage make sure you remove the part of code where you manually loadSDL, since it is not needed for static compilation make sure to use this subConfiguration: https://github.com/BindBC/bindbc-sdl#via-dub-subconfigurations and then make sure to add the lib to link in your "lflags" or as a "sourceFiles", both will work send us your dub.json file maybe some things are misconfigured?
Apr 24
parent reply Ishax <isaacbunsen gmail.com> writes:
On Saturday, 24 April 2021 at 16:02:27 UTC, russhy wrote:

 .a are object files for linux right?
Are they? I'm not very familiar with c++. I'm using windows. For that matter I should mention I'm using dub in vscode.
 send us your dub.json file maybe some things are misconfigured?
```sdl ... dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" sourceFiles "lib/libSDL2.a" "lib/libSDL_image.a" ``` The following works fine but links dynamically: ```sdl ... dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" libs "SDL2" "SDL_image" ```
Apr 24
parent reply russhy <russhy gmail.com> writes:
On Saturday, 24 April 2021 at 16:10:13 UTC, Ishax wrote:
 On Saturday, 24 April 2021 at 16:02:27 UTC, russhy wrote:

 .a are object files for linux right?
Are they? I'm not very familiar with c++. I'm using windows. For that matter I should mention I'm using dub in vscode.
 send us your dub.json file maybe some things are misconfigured?
```sdl ... dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" sourceFiles "lib/libSDL2.a" "lib/libSDL_image.a" ``` The following works fine but links dynamically: ```sdl ... dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" libs "SDL2" "SDL_image" ```
on windows, for static compilation you have to use "*.lib" files, not "*.a" also you are missing the ``` subConfigurations "bindbc-sdl" "staticBC" ```
Apr 24
next sibling parent reply Ishax <isaacbunsen gmail.com> writes:
On Saturday, 24 April 2021 at 16:13:12 UTC, russhy wrote:
 on windows, for static compilation you have to use "*.lib" 
 files, not "*.a"

 also you are missing the

 ```
 subConfigurations "bindbc-sdl" "staticBC"
 ```
So I have the lib files. It's failing with only an exit code. No window is appearing. It's using the same D code as with the dynamic setup which I was able to produce a window with. ```sdl dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" subConfigurations "bindbc-sdl" "staticBC" libs "SDL2" "SDL2_image" ```
Apr 24
next sibling parent reply Ishax <isaacbunsen gmail.com> writes:
Yet the .exe functions if I supply it with .dlls. Thats silly.
Apr 24
next sibling parent reply russhy <russhy gmail.com> writes:
On Saturday, 24 April 2021 at 16:48:49 UTC, Ishax wrote:
 It's using the same D code as with the dynamic setup which I 
 was able to produce a window with.
 Yet the .exe functions if I supply it with .dlls. Thats silly.
you didn't read my message Remove all the code used to loadSDL, it's not needed anymore with static compilation ``` dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" subConfigurations "bindbc-sdl" "staticBC" sourceFiles "my_sdl2.lib" "my_sdl2image.lib" `` ```D import bindbc.sdl; import core.stdc.stdio; int main() { SDL_Window* window = null; SDL_Surface* screenSurface = null; if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("could not initialize sdl2: %s\n", SDL_GetError()); return 1; } window = SDL_CreateWindow( "hello_sdl2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if (window == null) { printf("could not create window: %s\n", SDL_GetError()); return 1; } screenSurface = SDL_GetWindowSurface(window); SDL_FillRect(screenSurface, null, SDL_MapRGB(screenSurface.format, 0xFF, 0xFF, 0xFF)); SDL_UpdateWindowSurface(window); SDL_Delay(2000); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ``` This should work
Apr 24
parent reply Ishax <isaacbunsen gmail.com> writes:
On Saturday, 24 April 2021 at 17:20:06 UTC, russhy wrote:
 This should work
I just tried your code. I get: ```Program exited with code -1073741701``` I put the libraries straight into the project root this time. ```sdl name "sdl2try2" description "A minimal D application." authors "Isaac" copyright "Copyright © 2021, Isaac" license "proprietary" dependency "bindbc-sdl" version="~>0.1.0" versions "BindSDL_Static" subConfigurations "bindbc-sdl" "staticBC" sourceFiles "sdl2.lib" "sdl2_image.lib" ``` ```d import bindbc.sdl; import core.stdc.stdio; const SCREEN_WIDTH = 1024; const SCREEN_HEIGHT = 720; int main() { SDL_Window* window = null; SDL_Surface* screenSurface = null; if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("could not initialize sdl2: %s\n", SDL_GetError()); return 1; } window = SDL_CreateWindow( "hello_sdl2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if (window == null) { printf("could not create window: %s\n", SDL_GetError()); return 1; } screenSurface = SDL_GetWindowSurface(window); SDL_FillRect(screenSurface, null, SDL_MapRGB(screenSurface.format, 0xFF, 0xFF, 0xFF)); SDL_UpdateWindowSurface(window); SDL_Delay(2000); SDL_DestroyWindow(window); SDL_Quit(); return 0; } ```
Apr 24
parent reply russhy <russhy gmail.com> writes:
Try to add this in your dub file:

libs "user32" "gdi32" "shell32"
Apr 24
parent Ishax <isaacbunsen gmail.com> writes:
On Saturday, 24 April 2021 at 18:18:11 UTC, russhy wrote:
 Try to add this in your dub file:

 libs "user32" "gdi32" "shell32"
No change.
Apr 24
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 24 April 2021 at 16:48:49 UTC, Ishax wrote:
 Yet the .exe functions if I supply it with .dlls. Thats silly.
That's because you're linking with the import library. On Windows, static libraries and import libraries both have the `.lib` extension. The SDL development packages on the download page at http://libsdl.org/download-2.0.php do not include precompiled static libraries. They only include the DLLs and the import libraries. So you will either have to compile an SDL static library yourself or find someone who has made it available for download. And then when you link it with your executable, you will also need to link with every dependency that SDL has.
Apr 25
prev sibling parent reply ichneumwn <idonotenjoyemail idonotenjoyemail.org> writes:
On Saturday, 24 April 2021 at 16:44:09 UTC, Ishax wrote:
 So I have the lib files. It's failing with only an exit code. 
 No window is appearing. It's using the same D code as with the 
 dynamic setup which I was able to produce a window with.
 ```sdl
 dependency "bindbc-sdl" version="~>0.1.0"
 versions "BindSDL_Static"
 subConfigurations "bindbc-sdl" "staticBC"
 libs "SDL2" "SDL2_image"
 ```
Hi, Linux programmer here who just went through the process of working with dll's and lib's on windows. In my current understanding, there are three ways of working with libraries: 1. Load DLL's at runtime (LoadLibrary and such) 2. Linking with DLL's at compile time. The compiler adds code to automatically load the DLL's when your program starts. You do this by linking against an *import library*. These have a *.lib* extension 3. Linking against *static libraries*. These have a *.lib* extension Note that 2. & 3. are different things, but the files you need end in .lib in both cases. You'll have to check that your .lib files are actually static libraries, not import libraries. I am not sure if linking to outside sources is appropriate, so I will just copy-paste from stackoverflow: "Use the lib command. If it's static, lib will show you a pile of .obj files inside. Not so if it's am implib." lib /list foo.lib
Apr 24
parent reply Ishax <isaacbunsen gmail.com> writes:
On Saturday, 24 April 2021 at 18:55:33 UTC, ichneumwn wrote:
 "Use the lib command. If it's static, lib will show you a pile 
 of .obj files inside. Not so if it's am implib."

     lib /list foo.lib
This is the output of `lib /list SDL2.lib` ``` Microsoft (R) Library Manager Version 14.28.29913.0 Copyright (C) Microsoft Corporation. All rights reserved. SDL2.dll SDL2.dll ... ``` There's many many lines of just "sdl2.dll" My files are .lib files.
Apr 24
parent russhy <russhy gmail.com> writes:
On Saturday, 24 April 2021 at 19:25:47 UTC, Ishax wrote:
 On Saturday, 24 April 2021 at 18:55:33 UTC, ichneumwn wrote:
 "Use the lib command. If it's static, lib will show you a pile 
 of .obj files inside. Not so if it's am implib."

     lib /list foo.lib
This is the output of `lib /list SDL2.lib` ``` Microsoft (R) Library Manager Version 14.28.29913.0 Copyright (C) Microsoft Corporation. All rights reserved. SDL2.dll SDL2.dll ... ``` There's many many lines of just "sdl2.dll" My files are .lib files.
That must be it, make sure you compile your sdl as static library
Apr 24
prev sibling parent Mike Parker <aldacron gmail.com> writes:
On Saturday, 24 April 2021 at 16:13:12 UTC, russhy wrote:


 also you are missing the

 ```
 subConfigurations "bindbc-sdl" "staticBC"
 ```
That's not necessary when the version `BindSDL_Static` is provided. The `static` and `staticBC` subconfigurations both enable `-version=BindSDL_Static`, and `staticBC` is only required when you want to compile as a static binding with `-betterC`.
Apr 25