How can I embed a specific manifest file in a Windows DLL with a CMake build?

Viewed 13785

So I have a DLL that is being built with CMake that requires a specific manifest file to be embedded. In Visual Studio settings I can just add the manifest filename under Manifest Tool/Input and Ouput/Additional Manifest Files, and it works correctly. It seems like this is something that should be doable with CMake, but I have been unable to figure it out.

Any ideas on how I can accomplish this with CMake?

5 Answers

I just went through this exercise myself, which is what brought me to this page. Calvin1602's answer pretty much lays out the solution, but I had to finagle the syntax a bit to make it work for me. Here are the exact commands that finally worked:

if (WIN32)
    set(CMAKE_SHARED_LINKER_FLAGS /MANIFEST:NO)
endif()

add_custom_command(TARGET
                     odrmanager
                   POST_BUILD
                   COMMAND
                     "mt.exe" -manifest \"${CMAKE_CURRENT_SOURCE_DIR}\\odrmanager.dll.manifest\" -outputresource:\"${CMAKE_CURRENT_BINARY_DIR}\\odrmanager\\odrmanager.dll\"\;\#2
                   COMMENT
                     "Adding custom manifest containing MSVCRT80 dependency..." 
                  )

Note that you should use #1 in the mt.exe command when the target is an application, and #2 when it's a DLL (at least, as far as I understand it--it didn't work for me until I changed the 1 to a 2).

Also, you can use mt.exe to extract the original manifest from the DLL if you want/need to. The command looks like this:

mt -inputresource:odrmanager.dll;#2 -out:odrmanager.manifest

It's not too hard to hand-edit the output if you have a manifest file for the dependency you want to merge in. But I sorta like Calvin1602's trick of having Visual Studio do it for you if you're using Visual Studio solution files rather than nmake.

Related