简体   繁体   中英

cmake to link to dll without lib

I've been given a dll without libs. The dll comes with hpp and h files. I used dumpbin to create an exports.def file and lib to create a library.

I'm using the following 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 is also copied to the bin dir where the exe would be.

I get no linker errors with the lib, but no functions defined in the include headers have bodies found. 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:

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

Then when you link mytest to it, mytest will have a correct link line constructed and it will see the right headers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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