如何在Windows中将共享库* dll与CMake链接 [英] How to link shared library *dll with CMake in Windows

查看:485
本文介绍了如何在Windows中将共享库* dll与CMake链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个文件: library.dll和library.h ,其中包含我自己的项目中需要的一些代码。我正在Windows上使用Clion,应该在CMake上对其进行配置。

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.

我尝试过这种方式:

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)

它已编译但没有用。返回代码-1073741515

It compiled but didnt work. Returns code -1073741515

我该如何处理?

推荐答案

尽管这个问题很旧。您错误地定位了链接库。
target_link_libraries(test2 library.dll)是错误的。这是链接SDL2的示例。在主CMakeList.txt

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)

并位于库文件夹中。在此SDL中,添加CMakeLists.txt

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)

这篇关于如何在Windows中将共享库* dll与CMake链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆