How to get CMake to install PDB files for targets

2020-05-25 03:38发布

问题:

How do I get CMake to install the companion PDB files needed to debug Visual Studio generated DLL files and EXE files?

回答1:

I have struggled for a while trying to get a good answer to this question. I now think I have found one: use an install file command with the $<TARGET_PDB_FILE:tgt> generator expression (available in CMake 3.1.3 and newer). Specifically, the install command below seems to work. The command will copy the target ${PROJECT_NAME} pdb file to the target's installation bin directory.

install(FILES $<TARGET_PDB_FILE:${PROJECT_NAME}> DESTINATION bin OPTIONAL)

The command will install the pdb file for each configuration that generates a pdb file. By using OPTIONAL the install command will not generate an error if the source pdb file does not exist. This command is meant to be used for targets created with add_library(${PROJECT_NAME} ...) or add_executable(${PROJECT_NAME} ...) commands.

This is the best answer I have found. Please let me know if there is a better one. I found some difficult to understand documentation of the TARGET_PDB_FILE generator experession at the "Informational Expressions" section of the cmake-generator-expressions documentation.