# dll linking

**URL:** <https://discourse.cmake.org/t/dll-linking/4805>\
**Category:** Code\
**Created:** [January 10, 2022, 2:37am UTC](https://discourse.cmake.org/t/dll-linking/4805 "2022-01-10T02:37:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [January 10, 2022, 2:37am UTC](https://discourse.cmake.org/t/dll-linking/4805/1 "2022-01-10T02:37:32Z")

</div>

How do i link a dll? I tried few examples from the decumentation, but completely useless, no real world examples on how to load the library…

I tried to do add this in the cmakelist  
target\_link\_libraries(project\_name C:/Users/username/libabcd.dll)  
But errors occur, when i try to use it.

---

<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:** [January 10, 2022, 7:19am UTC](https://discourse.cmake.org/t/dll-linking/4805/2 "2022-01-10T07:19:26Z")

</div>

On windows you don’t link the shared object but the import library. That means you need to link to the associated “libabcd.lib”. The dll must be in your PATH (or one of the known directories) when you run your program then.

---

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [January 10, 2022, 8:34am UTC](https://discourse.cmake.org/t/dll-linking/4805/3 "2022-01-10T08:34:38Z")

</div>

And how to link that lib?

---

<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:** [January 10, 2022, 8:41am UTC](https://discourse.cmake.org/t/dll-linking/4805/4 "2022-01-10T08:41:41Z")

</div>

`target_link_libraries(target_name C:/Users/username/libabcd.lib)`  
Remarks:

1. It may be in a different path - you have to find the right file yourself.
2. Hardcoding paths is actually a bad idea

---

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [January 30, 2022, 4:48pm UTC](https://discourse.cmake.org/t/dll-linking/4805/5 "2022-01-30T16:48:51Z")

</div>

I left this thing for some time, but now i am trying again, and still there are problems. First of all, the documentation says, that in the target, there’s should be an exe, i don’t have any exe, so what do i put over there? I have 2 projects with cmakelist, and they are both libraries, one of them, requires some libs from the second project.

---

<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:** [January 30, 2022, 5:17pm UTC](https://discourse.cmake.org/t/dll-linking/4805/6 "2022-01-30T17:17:00Z")

</div>

There’s no problem the target being a library - it doesn’t have to be an exe.

The important question is: what are you actually doing and what is the error you’re getting?

---

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [January 30, 2022, 9:36pm UTC](https://discourse.cmake.org/t/dll-linking/4805/7 "2022-01-30T21:36:15Z")

</div>

As i said, i am trying to compile 2 libraries.

What i will do at this point instead, is turn off the jpeg support in the configuration, and it shouldn’t require the other library, but i can’t save the config after edit… I tried to turn off some stuff in cmake, and then hit build, but i don’t get anything usefull.

After i turn off what i want in the configuration, i hit build, and i don’t get any dll’s, nor do i get any .a files, what i get instead is some usless rubbish stuff. I tried to copy it back into the source file with cmakelist.txt, but then tit says the project is broken…

Because of that, i am not using cmake in order to compile, cause it doesn’t work what so ever.  
I use Qt in order to compile that, but in qt, i don’t have the configuration menu, i can’t turn anything on or off. What i want to do, is find the file where the data is stored, turn it manually off, and then compile it using cmake but in qt.

![img4](https://discourse.cmake.org/uploads/default/original/2X/e/e54b18867353667a33f4b1c94216c6bf1643dd62.png)  
But in which file are these options? I can’t configure it directly in Qt, in cmake it generates usless rubbish files, and it doesn’t change anything in the source configuration at all, so i have to turn them off manually.

---

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [January 30, 2022, 9:53pm UTC](https://discourse.cmake.org/t/dll-linking/4805/8 "2022-01-30T21:53:55Z")

</div>

And these are the files that cmake generates after hit build  
 ![img1](https://discourse.cmake.org/uploads/default/original/2X/3/3321aacdb73bdcf8cfa23882376872445157ad93.png)

Idk what to do with these things, in QT when i say build, it build the project, and i do get the dll’s and other usefull stuff.  
It doesn’t edit the configuration as well after i hit build, there are no changes made, if i reload the file, it’s the same old default configuration with everything turned on.

---

<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:** [January 31, 2022, 7:38am UTC](https://discourse.cmake.org/t/dll-linking/4805/9 "2022-01-31T07:38:43Z")

</div>

I don’t know what “in QT” means (cmake-gui? - I have like 0 experience with that) but what you’re doing with CMake looks like just building. The files are somewhere in the build tree but typically you should install the project to get nice standard layout and all the `find_package` stuff from other projects working.  
Build trees are not meant to be used by other projects.

---

<div class="post-metadata">

**Author:** ![kfsone](https://discourse.cmake.org/user_avatar/discourse.cmake.org/kfsone/32/1880_2.png) [@kfsone](https://discourse.cmake.org/u/kfsone)\
**Post date:** [January 31, 2022, 8:19pm UTC](https://discourse.cmake.org/t/dll-linking/4805/10 "2022-01-31T20:19:39Z")

</div>

@bambo09 It’s a little hard understanding what you’re trying to describe - you’ve verbally described operations you are doing rather than presenting a simple concrete CMakeLists, for instance.

As with many engineering tasks, you might want to take a step back and create a minimal verification example for yourself.

To create a library that produces a DLL:

```auto
CMAKE_MINIMUM_REQUIRED(VERSION 3.18)
PROJECT(DllTest)

# Tell cmake we want it to automate generating an export stub for the dll
SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

ADD_LIBRARY(
        mylib SHARED
        mylib.cpp
)

```

```auto
#include <iostream>

void mylib_fn() {
    std::cout << "mylib_fn\n";
}

```

```auto
> cmake -B out -G "Visual Studio 16 2019" .
...
> cmake --build out
Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

  Checking Build System
  Building Custom Rule C:/Users/oliver/tmp/cmake-dll/CMakeLists.txt
  mylib.cpp
  Auto build dll exports
     Creating library C:/Users/oliver/tmp/cmake-dll/out/Debug/mylib.lib and object C:/Users/oliver/tmp/cmake-dll/out/De
  bug/mylib.exp
  mylib.vcxproj -> C:\Users\oliver\tmp\cmake-dll\out\Debug\mylib.dll
  Building Custom Rule C:/Users/oliver/tmp/cmake-dll/CMakeLists.txt

```

and

```auto
[C:\Users\oliver\tmp\cmake-dll]
> dir .\out\Debug

    Directory: C:\Users\oliver\tmp\cmake-dll\out\Debug

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 1/31/2022 12:14 PM 57856 mylib.dll
-a--- 1/31/2022 12:14 PM 3628 mylib.exp
-a--- 1/31/2022 12:14 PM 6282 mylib.lib
-a--- 1/31/2022 12:14 PM 946176 mylib.pdb

```

Now you have a dll to experiment with linking. I would suggest adding a second library (shared or static) that links to it

```auto
ADD_LIBRARY(middle_lib SHARED middle_lib.cpp)
# hard-coded path is a bad idea outside an experiment.
TARGET_LINK_LIBRARIES(middle_lib c:/users/oliver/tmp/cmake-dll/out/Debug/mylib.lib)

```

and an executable to demonstrate it works

```auto
ADD_EXECUTABLE(experiment main.cpp)
TARGET_LINK_LIBRARIES(experiment middle_lib)

```

```auto
#include <iostream>

extern void middle_lib_fn();

int main() {
  std::cout << "main\n";
  middle_lib_fn();
}

```

---

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [February 8, 2022, 1:59pm UTC](https://discourse.cmake.org/t/dll-linking/4805/11 "2022-02-08T13:59:29Z")

</div>

Now i get this error

Can’t find lept.pc in any of C:/Strawberry/c/lib/pkgconfig  
use the PKG\_CONFIG\_PATH environment variable, or  
specify extra search paths via ‘search\_paths’

WHERE do i specify it?? I tried to put this PKG\_CONFIG\_PATH it in the cmakelist, and i get errors… comeone

---

<div class="post-metadata">

**Author:** ![bambo09](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/e95f7d/32.png) [@bambo09](https://discourse.cmake.org/u/bambo09)\
**Post date:** [February 8, 2022, 7:20pm UTC](https://discourse.cmake.org/t/dll-linking/4805/12 "2022-02-08T19:20:43Z")

</div>

Crap, never mind… Somehow after i thought i linked the library correctly, i had undefined references to errors, but this time during compile time, not building in cmake.

The reason why all this bullshit happens, was because of cmake… There was one path as variable to set, i set it multiple times, but after i hit generate or configure, the variable was set back to unknown path… I ignored it because i tought it was saved anyways, but it wasn’t. The only way to fix it, was to build the configuration, and then open the cmakecache and set the variable over there.

Kinda messed up considering that i lost at least 3 days because of it, somehow the rest of the configuration didn’t have these problems, it was only that one specific variable which somehow wasn’t saved, and the variable was the path to the required libraries. I tested it after and everything get compiled without problems.

---

<div class="post-metadata">

**Author:** ![tonyzzp](https://discourse.cmake.org/user_avatar/discourse.cmake.org/tonyzzp/32/2556_2.png) [@tonyzzp](https://discourse.cmake.org/u/tonyzzp)\
**Post date:** [June 30, 2022, 8:28am UTC](https://discourse.cmake.org/t/dll-linking/4805/13 "2022-06-30T08:28:22Z")

</div>

this is the right answer.
