-2

I have a C++ project like this

my_project
 |___ app
 |  |__ main.cpp
 |___ include
 |  |__ first.h
 |  |__ second.h
 |___ libs
 |  |__ first.dll
 |  |__ second.dll
 |___ CMakeLists.txt

Assume the target name from the main.cpp is my_target. I would like to link my_target with both the DLL libs in libs directory. The header files found in include directory are the definitions for the .DLL files found in libs directory. Can we use .DLL libs directly with C++ code on Windows? If it is possible, then how should we write the CMakeLists.txt for it? I have not found a helpful resource on this topic. Most of the content on the internet either says it is not possible to link to .DLL files or that we have to convert them into .LIB files first.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Arun Kumar
  • 634
  • 2
  • 12
  • 26
  • 1
    Short answer is: You cannot. – πάντα ῥεῖ Jun 21 '23 at 19:22
  • 1
    You can not with `msvc`. You can with `MinGW` so the answer may depend on which compiler you are using. – drescherjm Jun 21 '23 at 19:32
  • I have MinGW compiler. – Arun Kumar Jun 21 '23 at 19:37
  • 1
    Just call `target_link_libraries` with absolute path to the library file. E.g. `target_link_libraries(my_target PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/libs/first.dll)`. If your toolchain (compiler) supports linking with `dll`, then it will succeed. If your compiler doesn't support such linkage, then ... you need to chose other compiler or obtain/generate `.lib` files for link with them. – Tsyvarev Jun 22 '23 at 09:39

1 Answers1

0

GCC can do it with one of these two invocations. I forgot which is right, but it's one of them.

gcc file1.c file2.c path\to\library.dll

gcc file1.c file2.c -lpath\to\library.dll

Joshua
  • 40,822
  • 8
  • 72
  • 132