dll linking

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.

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.

1 Like

And how to link that lib?

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

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.

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?

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
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.

And these are the files that cmake generates after hit build
img1

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.

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.

@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:

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
)
#include <iostream>

void mylib_fn() {
    std::cout << "mylib_fn\n";
}
>  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

[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

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

ADD_EXECUTABLE(experiment main.cpp)
TARGET_LINK_LIBRARIES(experiment middle_lib)
#include <iostream>

extern void middle_lib_fn();

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

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

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.

this is the right answer.