简体   繁体   English

cmake 链接到 dll 没有 lib

[英]cmake to link to dll without lib

I've been given a dll without libs.我得到了一个没有库的 dll。 The dll comes with hpp and h files. dll 带有 hpp 和 h 文件。 I used dumpbin to create an exports.def file and lib to create a library.我使用dumpbin 创建exports.def 文件和lib 创建库。

I'm using the following CMakeLists.txt我正在使用以下 CMakeLists.txt

cmake_minimum_required ( VERSION 3.22 )

project ( mytest )

include_directories("${PROJECT_SOURCE_DIR}/libincludedir")

add_executable ( mytest main.cpp)

target_link_libraries ( mytest LINK_PUBLIC ${PROJECT_SOURCE_DIR}/libincludedir/anewlib.lib  )

The original dll and created lib and h and hpp files are all in the libincludedir.原来的 dll 和创建的 lib 和 h 和 hpp 文件都在 libincludedir 中。 Dll is also copied to the bin dir where the exe would be. Dll 也被复制到 exe 所在的 bin 目录中。

I get no linker errors with the lib, but no functions defined in the include headers have bodies found.我在 lib 中没有收到 linker 错误,但在包含头文件中定义的函数没有找到正文。 Types are Undefined.类型未定义。 Classes are incomplete.课程不完整。

How do I fix this?我该如何解决? Can I progress with what I was given or should I ask for more?我可以在得到的东西上取得进展还是我应该要求更多?

Thanks in advanced.先谢谢了。

Assuming that you created the .lib correctly, this is how you would set up something linkable:假设您正确创建了.lib ,这就是您设置可链接的方式:

cmake_minimum_required(VERSION 3.22)
project(example)

add_library(anewlib::anewlib SHARED IMPORTED)
set_target_properties(
  anewlib::anewlib
  PROPERTIES
  IMPORTED_IMPLIB "${PROJECT_SOURCE_DIR}/libincludedir/anewlib.lib"
  IMPORTED_LOCATION "${PROJECT_SOURCE_DIR}/libincludedir/anewlib.dll"
  IMPORTED_NO_SONAME "TRUE"
  INTERFACE_INCLUDE_DIRECTORIES "${PROJECT_SOURCE_DIR}/libincludedir"
)

add_executable(mytest main.cpp)
target_link_libraries(mytest PRIVATE anewlib::anewlib)

Always link to targets since they provide CMake with much more information than a raw library file.始终链接到目标,因为它们为 CMake 提供了比原始库文件更多的信息。 Here, we're telling CMake about the locations of both the .lib and the .dll , that it doesn't have a SONAME (a *nix only thing, anyway, but good to include), and the include path that linkees should use to find its associated headers.在这里,我们告诉 CMake 关于.lib.dll的位置,它没有 SONAME (无论如何,只有 *nix 的东西,但很好包含),以及链接者应该包含的包含路径用于查找其关联的标头。

Then when you link mytest to it, mytest will have a correct link line constructed and it will see the right headers.然后,当您将mytest链接到它时, mytest将构建一个正确的链接行,并且它将看到正确的标题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM