3

I am using CMake to build a project for MSVC.

All I have is a .def file with exported functions from a dll library. My project doesn't have access to a dll or a source code. And I need an import library to eliminate unresolved externals. The SDK I'm building my project for, uses only header files and import libraries. However some libraries are not built, so I must build them alone without a source code or a DLL libraries (they are packed with the product, not SDK).

I can build an import library with following command, I used it as a pre-link event in the VS project:

lib /def:<def path> /OUT:<lib name> /MACHINE:<machine type>

Is there a way for the CMake as well? I've tried commands like:

add_library(Ilib STATIC empty.cpp expfile.def)
add_library(Ilib SHARED empty.cpp expfile.def)
add_library(Ilib STATIC expfile.def)
set_target_properties(Ilib PROPERTIES LINKER_LANGUAGE CXX)

But none of them are working.

I think, the only solution would be to create an execute_process or add_custom_command command.

  • Did you see this answers to [this question](https://stackoverflow.com/q/225432/3987854)? These appear to focus on exporting, but may be helpful. Also, there is a link to an article [here](https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/). – Kevin Dec 02 '19 at 21:57
  • Thank you kittles, I had read them both, they are focused on generating a .def file from built .dlls or tagets. But I have seen `add_custom_command`. I could make a pre-link event into a generated project, in a worst case scenario. – Lukáš Kužel Dec 03 '19 at 07:32

1 Answers1

1

This post was the top search result when I was searching for this last night and I couldn't find anything useful. Here's what I ended up with if it's useful for someone (or for future me looking for this again):

(Assumes the def file is next to the CMakeLists.txt)

project(msvcrt)

add_custom_command(
  OUTPUT msvcrt.lib
  COMMAND lib.exe /machine:X64 /def:${CMAKE_CURRENT_SOURCE_DIR}/msvcrt.def /out:msvcrt.lib
  DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/msvcrt.def
  VERBATIM)

add_custom_target(msvcrtlib ALL
    DEPENDS msvcrt.lib
    VERBATIM)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/msvcrt.lib DESTINATION foobar)