dll linking

@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