How to link shared library *dll with CMake in Windows

怎甘沉沦 提交于 2020-02-25 23:11:50

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/42456284/how-to-link-shared-library-dll-with-cmake-in-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!