14

I have 2 files: library.dll and library.h with some code that I need in my own project. I'm working on Windows with Clion where I should config this with CMake.

I tried this way:

cmake_minimum_required(VERSION 3.6)
project(test2)

set(CMAKE_CXX_STANDARD 11)
link_directories(C:\\Users\\Johny\\CLionProjects\\test2)

set(SOURCE_FILES main.cpp)
add_executable(test2 ${SOURCE_FILES})

target_link_libraries(test2 library.dll)

It compiled but didnt work. Returns code -1073741515

How can I handle with it?

JohnyBe
  • 313
  • 1
  • 2
  • 10
  • Linking is correct. (It is not perfect, but correct). The problem is somewhere in your executable or in the library. – Tsyvarev Feb 25 '17 at 19:02
  • 2
    This question should not be marked as a duplicate. This question the first google search hit for how to add libraries to cmake, it's not answered, and the supposedly duplicate link is terribly outdated and does not handle DLLs. – RAM Mar 11 '22 at 16:10

1 Answers1

10

Although this question is old. You are targeting the link library wrongly. target_link_libraries(test2 library.dll) is wrong. This is an example linking SDL2. In the main CMakeList.txt

cmake_minimum_required(VERSION 3.12)
project(GraphicTest)

set(CMAKE_CXX_STANDARD 11)

include_directories("${PROJECT_SOURCE_DIR}/SDL")
add_subdirectory(SDL)

add_executable(GraphicTest main.cpp)
target_link_libraries(GraphicTest SDL2)

and in the library folder. Here SDL, add a CMakeLists.txt

message("-- Linking SDL")
add_library(SDL2 SDL2.dll)
set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE C)
AKJ
  • 950
  • 2
  • 13
  • 18
  • 6
    Does not work for me. I get `LINK : fatal error LNK1104: cannot open file 'external_dll\Debug\hello.lib'` (using subdirectory external_dll instead of SDL and hello instead of SDL2). – Ingo Aug 13 '21 at 13:55
  • 6
    The line `add_library(SDL2 SDL2.dll)` is wrong: it defines a library which should be **built** by CMake. For already-built libraries IMPORTED library target should be used. Also, it is impossible to link with `.dll` library file: it should be `.lib` file (import file). – Tsyvarev Mar 07 '22 at 08:31