# How can dynamic DLL libraries be found at runtime?

**URL:** https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005
**Category:** Usage
**Tags:** os:windows
**Created:** [September 19, 2023, 6:56am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005 "2023-09-19T06:56:33Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![xhc-code](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhc-code/32/3842_2.png) [@xhc-code](https://discourse.cmake.org/u/xhc-code)
#### Post date: [September 19, 2023, 6:56am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/1 "2023-09-19T06:56:33Z")

</div>

I have a DLL library that is external and has links configured\_ Directory can be compiled and linked normally, but during runtime, it will warn that xxx.dll is not found, and through ldd xxx.exe, it will also prompt that the dynamic DLL library is not found. How can I find this DLL library during runtime? The running environment is window, can only modify the PATH directory to find it? Because this DLL belongs to another program, the location of the DLL cannot be moved.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 19, 2023, 1:41pm UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/2 "2023-09-19T13:41:29Z")

</div>

Windows (PE-COFF) provides nothing like the RPATH mechanism on macOS (Mach-O) or Linux (ELF). Using `PATH` before startup or adding search paths to the `LoadLibraryEx` are the only ways I know of doing this.

---

<div class="post-metadata">

### Author: ![xhc-code](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhc-code/32/3842_2.png) [@xhc-code](https://discourse.cmake.org/u/xhc-code)
#### Post date: [September 20, 2023, 2:43am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/4 "2023-09-20T02:43:05Z")

</div>

This is really uncomfortable. I am very curious about how cmake searches for the library downloaded through pacman in mingw64 through pkg-config. Do you know this? Because I didn’t see the lib path in mingw added to PATH in the environment variable, I don’t know why I can search for the dll library through pkg-config.

---

<div class="post-metadata">

### Author: ![xhc-code](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhc-code/32/3842_2.png) [@xhc-code](https://discourse.cmake.org/u/xhc-code)
#### Post date: [September 20, 2023, 2:49am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/5 "2023-09-20T02:49:03Z")

</div>

Does window support setting the LD\_LIBRARY\_PATH variable to search for class library paths at runtime?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [September 20, 2023, 6:54am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/6 "2023-09-20T06:54:14Z")

</div>

On Linux you have PATH and LD\_LIBRARY\_PATH. On Windows there is only PATH, which combines the functions of the two.  
In short: you need to add appropriate paths to the PATH variable on Windows.

---

<div class="post-metadata">

### Author: ![xhc-code](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhc-code/32/3842_2.png) [@xhc-code](https://discourse.cmake.org/u/xhc-code)
#### Post date: [September 20, 2023, 7:08am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/7 "2023-09-20T07:08:29Z")

</div>

Okay, are there any relevant reference documents?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [September 20, 2023, 7:22am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/8 "2023-09-20T07:22:47Z")

</div>

I don’t know any.  
I am sure it’s described somewhere on the Microsoft sites, including the default places searched, and the lookup order.

---

<div class="post-metadata">

### Author: ![xhc-code](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhc-code/32/3842_2.png) [@xhc-code](https://discourse.cmake.org/u/xhc-code)
#### Post date: [September 20, 2023, 7:27am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/9 "2023-09-20T07:27:57Z")

</div>

All right.

---

<div class="post-metadata">

### Author: ![xhc-code](https://discourse.cmake.org/user_avatar/discourse.cmake.org/xhc-code/32/3842_2.png) [@xhc-code](https://discourse.cmake.org/u/xhc-code)
#### Post date: [September 22, 2023, 4:59am UTC](https://discourse.cmake.org/t/how-can-dynamic-dll-libraries-be-found-at-runtime/9005/10 "2023-09-22T04:59:50Z")

</div>

[There is explanatory information about the DLL search path and import library](https://www.cnblogs.com/XingXiaoMeng/p/17721031.html%5D)
