Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link shared library *dll with CMake in Windows

Tags:

c++

cmake

clion

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?

like image 961
JohnyBe Avatar asked Feb 25 '17 12:02

JohnyBe


1 Answers

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)
like image 104
AKJ Avatar answered Sep 30 '22 22:09

AKJ