0

I am trying to link with a DLL executable using CMake. This works fine on Linux (of course with .so), but on Windows, it seems to be tricky. I thought it might be some other dependencies, so I tried creating a simple test program, but I couldn't get that to work either.

My folder structure is simple:

main.cpp
CmakeLists.txt
lib/libtest.dll
lib/CMakeLists.txt

I based this structure on the answer to another question that was asked. Unfortunately, when I build, it it fails.

The content of the lib/CMakeLists.txt are:

message("-- Linking Test")
add_library(TEST libtest.dll)
set_target_properties(TEST PROPERTIES LINKER_LANGUAGE C)

And the content of the main CMakeLists.txt are:

cmake_minimum_required(VERSION 3.15)
project(Test_program)

set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS "-DDebug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )

# Contain the header files for testlib
include_directories("C:/test/utils/")
add_subdirectory(lib)


add_executable(Test_program main.cpp)
target_link_libraries(Test_program TEST)

This results in the following output:

-- Linking Test
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/akda/Test_program/cmake-build-debug
[ 33%] Linking C static library libTEST.a
[ 33%] Built target TEST
[ 66%] Building CXX object CMakeFiles/Test_program.dir/main.cpp.obj
[100%] Linking CXX executable Test_program.exe
CMakeFiles\Test_program.dir/objects.a(main.cpp.obj): In function `powerOn()':
C:/Users/akda/Test_program/main.cpp:8: undefined reference to `My_Library_Function'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\Test_program.dir\build.make:87: Test_program.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:77: CMakeFiles/Test_program.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2

Using tools as DLLExportViewer, I can confirm that the DLL, in fact, does contain the function called. What am I doing wrong? I'll never claim to be an expert in CMake, but I really can't see what's wrong.

Update with link to .a file As pointed out, I could try linking with the .a file. I've added this to the library folder, and changed the main CMakeLists.txt to the following:

cmake_minimum_required(VERSION 3.15)
project(Test_program)

set(CMAKE_CXX_STANDARD 14)
set(GCC_COVERAGE_COMPILE_FLAGS "-DDebug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}" )


include_directories("C:/test/utils/")

find_library(TEST ${CMAKE_SOURCE_DIR}/lib/libtest.a)


add_executable(Test_program main.cpp)
target_link_libraries(Test_program ${TEST})

And this ends up in the same way, with the same errors.

Benjamin Larsen
  • 560
  • 1
  • 7
  • 21
  • 1
    You should not add `libtest.dll` as a source for a library. Instead you should find its export library (e.g. `libtest.a`) and your application should link with it. – Some programmer dude Feb 04 '20 at 11:16
  • Could it have something to do with https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport? – Benjamin Bihler Feb 04 '20 at 11:17
  • @Someprogrammerdude if you see the output it actually creates a ".a" file. Further, I have tried linking directly to the .a file, which I also have. The ends in the same result. – Benjamin Larsen Feb 04 '20 at 11:34
  • 1
    With MinGw64 you can directly link to a .dll, you don't need an export library. But you have to export the methods that you want to use from the outside. Therefore check the link I have posted. – Benjamin Bihler Feb 04 '20 at 11:59
  • @BenjaminLarsen Did you find your answer ? I found plenty of different answers and nothing worked. – ArthurLambert Jun 13 '22 at 14:51

0 Answers0