2

When you create a DLL in CMake on windows you need you to link dependencies by adding the *.lib file to the target link libraries. For instance, if we have a DLL target called dll_target

add_library(dll_target SHARED ${sources})

That depends on target depA, as I understand it, standard practice would be to link against depA.lib.

target_link_libraries(dll_target PRIVATE :C:/full/path/to/depA/install/lib/depA.lib")

This shouldn't be confused with depA.lib the static library, but the depA.lib associated with depA.dll which is built when you build depA.dll. The latter is needed at runtime.

On Linux, I have been able to build all of my dependencies as .a files and then use these static libraries to construct a shared library (.so). However, when I try the same with windows, I get a slew of linker errors (mostly LNK2019 but the occasional LNK2001).

I've been searching for this answer for days now: How can I use static libraries on windows to construct a self-contained DLL, so that it has no external dependencies?

CiaranWelsh
  • 7,014
  • 10
  • 53
  • 106
  • In respect of shared and static libraries, Windows has understanding similar to one of the Linux: when create a shared library, all static libraries are "embedded" into it, so these static libraries are not needed when use the created shared library. But.. on Windows `.lib` file doesn't necessary represent a **static** library, it could also represent an **export** file for the **shared** library. I suspect this is your actual problem... – Tsyvarev Jul 30 '20 at 12:15
  • I'm confident that the libraries I'm trying to link are actually static libraries. This is because I have not built the dynamic versions of my dependencies. I know I'm able to use the `export` library (thanks for giving me its name) but this requires the dll be present at runtime (right?). So what I want to know is how to build the dll so that its dependencies are embedded, and not needed at runtime? – CiaranWelsh Jul 30 '20 at 12:37
  • May be, create [mcve] with exact names of functions/files, so we could reproduce your problem? – Tsyvarev Jul 30 '20 at 12:39

0 Answers0