16

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?

2
  • Linking is correct. (It is not perfect, but correct). The problem is somewhere in your executable or in the library. Commented Feb 25, 2017 at 19:02
  • 4
    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. Commented Mar 11, 2022 at 16:10

1 Answer 1

13

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

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).
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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.