www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - DUB: How to link an external library on Windows 10?

reply Ki Rill <rill.ki yahoo.com> writes:
I have a Raylib project on Windows using DUB. I've added raylib-d 
via `dub add`. But what I can't figure out is how to tell DUB to 
link against raylib library.

I have the following project structure:
```
-> source
---> app.d
-> libraylib.a
-> raylib.dll
-> etc...
```

I'd like to use either .a or .dll. Do you have any ideas?
Aug 27 2021
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 27 August 2021 at 13:21:04 UTC, Ki Rill wrote:
 I have a Raylib project on Windows using DUB. I've added 
 raylib-d via `dub add`. But what I can't figure out is how to 
 tell DUB to link against raylib library.

 I have the following project structure:
 ```
 -> source
 ---> app.d
 -> libraylib.a
 -> raylib.dll
 -> etc...
 ```

 I'd like to use either .a or .dll. Do you have any ideas?
raylib.a isn't going to get you anywhere. You'll run into issues mixing MinGW-compiled libraries. If raylib doesn't ship precompiled VS binaries, then, assuming you have Visual Studio (or the MS Build tools) installed, you should compile Raylib with that same version. Then you'll have a new raylib.dll and a raylib.lib. Add raylib.lib to your dub config via the "libs" directive.
Aug 27 2021
parent reply Ki Rill <rill.ki yahoo.com> writes:
On Friday, 27 August 2021 at 13:33:25 UTC, Mike Parker wrote:
 On Friday, 27 August 2021 at 13:21:04 UTC, Ki Rill wrote:
 I have a Raylib project on Windows using DUB. I've added 
 raylib-d via `dub add`. But what I can't figure out is how to 
 tell DUB to link against raylib library.

 I have the following project structure:
 ```
 -> source
 ---> app.d
 -> libraylib.a
 -> raylib.dll
 -> etc...
 ```

 I'd like to use either .a or .dll. Do you have any ideas?
raylib.a isn't going to get you anywhere. You'll run into issues mixing MinGW-compiled libraries. If raylib doesn't ship precompiled VS binaries, then, assuming you have Visual Studio (or the MS Build tools) installed, you should compile Raylib with that same version. Then you'll have a new raylib.dll and a raylib.lib. Add raylib.lib to your dub config via the "libs" directive.
I have downloaded the pre-compiled binaries from the official [Raylib ](https://github.com/raysan5/raylib/releases/tag/3.7.0) repo. I'm not using Visual Studio. Only dub and a text editor.
Aug 27 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 27 August 2021 at 13:41:56 UTC, Ki Rill wrote:

 I have downloaded the pre-compiled binaries from the official 
 [Raylib ](https://github.com/raysan5/raylib/releases/tag/3.7.0) 
 repo.

 I'm not using Visual Studio. Only dub and a text editor.
But assuming you are on a 64-bit system, the DMD will use Visual Studio's linker if you have it installed. If you don't, it will use the lld linker it ships with. Either way, you need to raylib to be compiled with he same version of the MS Build tools so that you don't have conflicts the the vs runtime.
Aug 27 2021
parent Ki Rill <rill.ki yahoo.com> writes:
On Friday, 27 August 2021 at 13:54:25 UTC, Mike Parker wrote:
 But assuming you are on a 64-bit system, the DMD will use 
 Visual Studio's linker if you have it installed. If you don't, 
 it will use the lld linker it ships with. Either way, you need 
 to raylib to be compiled with he same version of the MS Build 
 tools so that you don't have conflicts the the vs runtime.
Indeed, DMD used the lld linker.
Aug 27 2021
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/21 9:21 AM, Ki Rill wrote:
 I have a Raylib project on Windows using DUB. I've added raylib-d via 
 `dub add`. But what I can't figure out is how to tell DUB to link 
 against raylib library.
 
 I have the following project structure:
 ```
 -> source
 ---> app.d
 -> libraylib.a
 -> raylib.dll
 -> etc...
 ```
 
 I'd like to use either .a or .dll. Do you have any ideas?
I spent a lot of time trying to figure this out (someone in discord really wanted to link statically for some reason). I FINALLY got a statically linked exe, but it's not worth the effort. I had to: 1. update my msvc build tools to match what was used for the official release (alternatively, you can rebuild the raylib library with your tools, otherwise you get cryptic errors like `fatal error C1900: Il mismatch between 'P1' version '20210113' and 'P2' version '20190715'`). 2. pass in cryptic linker options like `/NODEFAULTLIB:libcmt`, etc. 3. Link against extra libraries until the linker errors disappear (google search for missing symbols to see what libraries those symbols are in). My eventual resulting dub.json looked like (you can guess where I put things): ```json { "name": "rps-explosion", "dependencies": { "jsoniopipe": "~>0.1.3", "enet-d": "~>0.0.1", "raylib-d": "~>3.1.0" }, "libs": ["enet", "raylib", "ws2_32", "winmm", "msvcrt", "user32", "gdi32"], "lflags": ["/LIBPATH:C:\\dprojects\\enet-1.3.17", "/LIBPATH:C:\\dprojects\\raylib-3.7.0_win64_msvc16\\lib", "/NODEFAULTLIB:libcmt", "/NODEFAULTLIB:libvcruntime"] } ``` This config also included `enet-d` so some of the linker options are for that lib. In the end, I got it to build and run, but I'd highly recommend just linking against the `raylibdll.lib` and using the dll. -Steve
Aug 27 2021
next sibling parent reply Ki Rill <rill.ki yahoo.com> writes:
On Friday, 27 August 2021 at 13:54:18 UTC, Steven Schveighoffer 
wrote:
 In the end, I got it to build and run, but I'd highly recommend 
 just linking against the `raylibdll.lib` and using the dll.

 -Steve
Steve, thank you! I got it working with `raylibdll.lib`! SOLUTION: 1. `dub init` 2. `dub add raylib-d` 3. download `raylib` pre-compiled [binaries](https://github.com/raysan5/raylib/releases) for Microsoft Visual Studio (msvs) 4. put `raylib.dll` and `raylibdll.lib` into your project's folder (into the same directory, where you have `dub.json`) 5. add `"libs":["raylibdll"]` section into dub.json Compile and run. It should work.
Aug 27 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/21 10:35 AM, Ki Rill wrote:
 On Friday, 27 August 2021 at 13:54:18 UTC, Steven Schveighoffer wrote:
 In the end, I got it to build and run, but I'd highly recommend just 
 linking against the `raylibdll.lib` and using the dll.
Steve, thank you! I got it working with `raylibdll.lib`!
Yes, either 3.5.0 or 3.7.0, they now build with 2 types of libs, static and dynamic. The raylib.lib file is for static linking, the raylibdll.lib file is for DLL. Glad you got it working!
 4. put `raylib.dll` and `raylibdll.lib` into your project's folder (into 
 the same directory, where you have `dub.json`)
You know, I taught a class using raylib and D (I learned a lot from your video series, thanks!), and I didn't even think about just copy the libraries to your project directory as a "step". Instead I had them put in the /LIBDIR flags to wherever they installed it. This way is MUCH easier, I think I'll switch to that. dub in general has some rough edges when linking against libraries that aren't in default locations. Having to edit the dub.json file is sub-par. -Steve
Aug 27 2021
prev sibling parent reply Ki Rill <rill.ki yahoo.com> writes:
On Friday, 27 August 2021 at 13:54:18 UTC, Steven Schveighoffer 
wrote:
 [...]
How do I tell DUB where to look for `raylibdll.lib` and `raylib.dll`? Via `lflags` section? What if I put them in a different folder instead of the project's directory?
Aug 27 2021
parent reply Mike Parker <aldacron gmail.com> writes:
On Friday, 27 August 2021 at 14:46:56 UTC, Ki Rill wrote:
 On Friday, 27 August 2021 at 13:54:18 UTC, Steven Schveighoffer 
 wrote:
 [...]
How do I tell DUB where to look for `raylibdll.lib` and `raylib.dll`? Via `lflags` section? What if I put them in a different folder instead of the project's directory?
Yes. The path goes in the lflags directive using whatever the linker-specific flag is. I assume for lld it's `-Lpath`. For MS link it's `/LIBPATH:path`.
Aug 27 2021
parent reply Ki Rill <rill.ki yahoo.com> writes:
On Friday, 27 August 2021 at 14:52:15 UTC, Mike Parker wrote:
 On Friday, 27 August 2021 at 14:46:56 UTC, Ki Rill wrote:
 On Friday, 27 August 2021 at 13:54:18 UTC, Steven 
 Schveighoffer wrote:
 [...]
How do I tell DUB where to look for `raylibdll.lib` and `raylib.dll`? Via `lflags` section? What if I put them in a different folder instead of the project's directory?
Yes. The path goes in the lflags directive using whatever the linker-specific flag is. I assume for lld it's `-Lpath`. For MS link it's `/LIBPATH:path`.
I've added lfags: ``` "lflags": ["/LIBPATH:C:\\Users\\Username\\Desktop\\test\\source\\"] ``` But now it cannot find the following: ``` msvcrt120.lib OLDNAMES.lib shell32.lib ``` I think `lfags` overrides the default search path and I need to add it manually as well. But what is that path on Windows?
Aug 27 2021
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/21 11:19 AM, Ki Rill wrote:
 On Friday, 27 August 2021 at 14:52:15 UTC, Mike Parker wrote:
 On Friday, 27 August 2021 at 14:46:56 UTC, Ki Rill wrote:
 On Friday, 27 August 2021 at 13:54:18 UTC, Steven Schveighoffer wrote:
 [...]
How do I tell DUB where to look for `raylibdll.lib` and `raylib.dll`? Via `lflags` section? What if I put them in a different folder instead of the project's directory?
Yes. The path goes in the lflags directive using whatever the linker-specific flag is. I assume for lld it's `-Lpath`. For MS link it's `/LIBPATH:path`.
To clarify, the .dll file's path is not embedded into the binary. You have to add it's path to your "Path" environment variable in order for your game to load it. But you do need the /LIBPATH option to tell it where to find the .lib file.
 
 I've added lfags:
 ```
 "lflags": ["/LIBPATH:C:\\Users\\Username\\Desktop\\test\\source\\"]
 ```
 
 But now it cannot find the following:
 ```
 msvcrt120.lib
 OLDNAMES.lib
 shell32.lib
 ```
 
 I think `lfags` overrides the default search path and I need to add it 
 manually as well. But what is that path on Windows?
That shouldn't happen. I've never had to tell it where the default libraries are. For sure the lflags does NOT override the default library search paths. I suspect your MSVC installation is bad, or there are some other switches causing problems. -Steve
Aug 27 2021
parent reply Ki Rill <rill.ki yahoo.com> writes:
On Friday, 27 August 2021 at 15:24:14 UTC, Steven Schveighoffer 
wrote:
 I suspect your MSVC installation is bad, or there are some 
 other switches causing problems.

 -Steve
Hmm... well, I will use the default setup and think about it later. I mostly use Linux, Windows realm is an uncharted territory for me.
Aug 27 2021
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 8/27/21 11:43 AM, Ki Rill wrote:
 On Friday, 27 August 2021 at 15:24:14 UTC, Steven Schveighoffer wrote:
 I suspect your MSVC installation is bad, or there are some other 
 switches causing problems.
Hmm... well, I will use the default setup and think about it later. I mostly use Linux, Windows realm is an uncharted territory for me.
Take my diagnoses with a grain of salt -- I mostly use MacOS and Linux, and I'm mostly lost on Windows. I was proficient with Visual C++ 5 or so a long time ago ;) -Steve
Aug 27 2021