30

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Phil
  • 5,822
  • 2
  • 31
  • 60

1 Answers1

37

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.

Phil
  • 5,822
  • 2
  • 31
  • 60
  • 4
    This only works with linker generated .pdbs , and so don't works with STATIC libraries according to the docs. https://cmake.org/cmake/help/v3.3/prop_tgt/PDB_OUTPUT_DIRECTORY.html#prop_tgt:PDB_OUTPUT_DIRECTORY – Noki Nov 13 '18 at 10:58
  • @Noki, you are right. This answer only works for shared libraries. Have you found a way for static libraries? – Ali Dec 18 '18 at 06:48
  • 6
    @Ali For static libraries you can use Compile Pdbs. You should use target properties, COMPILE_PDB_OUTPUT_DIRECTORY and COMPILE_PDB_NAME, personnally I use library output directory, and library name because it's better to access in install method. And then I use install(FILES "$/${Name}.pdb" DESTINATION "lib/static/$/${Name}" OPTIONAL) but the choice is all yours. Doc Ref : https://cmake.org/cmake/help/v3.3/prop_tgt/COMPILE_PDB_OUTPUT_DIRECTORY.html#prop_tgt:COMPILE_PDB_OUTPUT_DIRECTORY – Noki Dec 18 '18 at 15:40
  • 9
    It may worth mentioning that it is possible to control build types for which the files are installed: `install(FILES $ CONFIGURATIONS "RelWithDebInfo" DESTINATION bin)` – Innokentiy Alaytsev Jan 24 '19 at 14:33
  • Can we pass this info through command line argument? Because don't want to modify existing cmakefile. – Prasaathviki Feb 26 '22 at 06:10