75

In VS2010 there is an option to generate debug info for exes/dlls under linker but no such option under librarian for libs. Is the debug info embedded in the static library?

There is an option in the C/C++ properties for Program Database File Name for libs, exes, and dlls. By default it goes into my intermediate directory and is named the project name for libs, but is named vc$(PlatformToolsetVersion).pdb for exes/dlls. What's the pdb from this option and how does it differ from the pdb in the linker option?

If I am supplying a library with libs and headers how do I supply debug symbols to a user of my library?

5 Answers 5

60

If you use /ZI or /Zi (C/C++ -> General -> Debug Information Format), then the vc$(PlatformToolsetVersion).pdb is created, which contains the debug info for all of the .obj files created. If alternately you use /Z7, the debug info will be embedded into the .obj file, and then embedded into the .lib. This is probably the easiest way to distribute the debug info for a static library.

I wouldn't advise distributing a static library, however, since it's generally tied to a specific version of the compiler.

11
  • 1
    /Z7 is an argument to the compiler. You can use it with any code file you're compiling, regardless of whether the .obj file will be linked into an .exe or .dll. You need the debugging information from compile time to be able to produce a .pdb for the .exe/.dll at link time. /Z7 says to put it in the .obj file itself, whereas the default behavior is to place it in vc$(version).pdb.
    – mloar
    Sep 29, 2011 at 0:32
  • 2
    This doesn't seem to work for me with VS 2013 for some reason. VS is failing to load debug information for the .lib file sources. I have /Z7 for both .lib and .exe projects, and /debug in my .exe project link options. Dec 4, 2013 at 19:36
  • 1
    Was anyone able to fix this for VS 2013? I am also unable to debug into my static library. The static library is built externally using CMake and NMake. I tried switching over to /Z7 from /Zi, but it didn't change anything.
    – petrsnd
    Jul 8, 2014 at 4:11
  • 3
    @mloar Regarding your last paragraph, aren't dlls also tied to specific compiler version? (stackoverflow.com/questions/3427885/…)
    – Pedro Reis
    Jan 29, 2016 at 9:49
  • 4
    C++ DLLs are specific to the compiler version.
    – evoskuil
    May 18, 2016 at 2:57
48

Expanding upon previous answers, for those who need the full how-to (VS 2013 minimum).

Note that this should address comments ^^above regarding VS2013 issues.

Method 1: The Program Database (.pdb) Way (/Zi or /ZI)

  1. Static Lib Project: Generate a pdb with same name as your static lib:

    • Open Solution Explorer from the View menu.
    • Right click your static lib project, select Properties
    • Edit Configuration Properties->C/C++->General->Debug Information to /Zi or /ZI
      • Note that /ZI allows "Edit and Continue" editing during debugging
    • Edit Configuration Properties->C/C++->Output Files->Program Database File Name to $(OutDir)$(TargetName).pdb
    • Now compile it, and note where YourLib.lib and YourLib.pdb are.
  2. Application Project: Link your executable with the static lib, and new PDB file:

    • Again, navigate to project properties, but this time, for your Application project
    • Again, edit Debug Information property as needed.
    • Edit Configuration Properties->Linker->General->Additional Library Directories, adding your own "libs" directory, or whatever directory you plan to keep/copy your YourLib.lib and YourLib.pdb files.
    • Edit Configuration Properties->Linker->Input->Additional Dependencies, adding YourLib.lib (no path in front)
    • Now copy both YourLib.lib and YourLib.pdb to the directory you specified above.

Method 2: The Embedded Symbols (no .pdb) Way (/Z7)

  1. Static Lib Project: Generate a static lib with embedded debug symbols

    • As in Method 1, navigate to project properties
    • As in Method 1, modify your Debug Information, but this time to/Z7
    • As in Method 1, compile and note where YourLib.lib is generated.
  2. Application Project: Link you executable with the static lib

    • As in Method 1, navigate to project properties
    • As in Method 1, modify your Debug Information property as needed
    • As in Method 1, edit Additional Library Directories
    • As in Method 1, edit Additional Dependencies
    • Now copy YourLib.lib to the directory specified in Additional Library Directories

Discussion:

  • Advantages of Z7? It's simpler, and the "Single-file" way of doing it. All the debug info is in the lib file.
  • Disadvantages of Z7? File size on-disk, link times, incompatible with "Minimal rebuild" (/Gm) feature, does not allow "Edit and Continue", older format (e.g. older paradigm)
  • Why don't I specify Debug Information Setting for Application Project? This post is concerned with how to get debug working in static lib code. The same "Method 1 vs Method 2" choice applies for the Application project as well.
7
  • Is it possible for PDB files to be in a separate folder from LIB? I specify both in my library directories, but it's not picking them up.
    – riv
    Sep 12, 2018 at 8:38
  • @riv there are a few options, see here.
    – bunkerdive
    Sep 13, 2018 at 13:47
  • @bunkerdive we want to set Configuration Properties->C/C++->Output Files->Program Database File Name using comand-line in make-file, so we ask is /PDB the right command or is that for something different?
    – Top-Master
    Dec 29, 2018 at 6:35
  • 1
    @Top-Master: if you see the answer below, you can set the pdb name like that, and then inspect the actual build commands run in the build output, to see what flags/params were used.
    – bunkerdive
    Apr 26, 2019 at 23:13
  • 1
    @bunkerdive: Thanks. I figured out my problem and explained it here: stackoverflow.com/questions/68507293/… Jul 27, 2021 at 16:49
17

I notice in VS2013 it is possible to set the program database file name in the C/C++ Output Files tab. Changing it from the default to something like $(OutDir)$(TargetName).pdb resolves the issue

1
  • Seems $(IntDir)vc$(PlatformToolsetVersion).pdb will make /Z7 invalid and need set to $(OutDir)$(TargetName).pdb to let it take effect in some case of update project from VS2010 to VS2015.
    – zzy
    Mar 30, 2018 at 9:19
5

Static libraries are implemented into the programs that use them.

If the program that uses them is using debug symbols, the compiled library code in that program will have symbols too.

PDB info from wikipedia:

When debug symbols are embedded in the binary itself, the file can then grow significantly larger (sometimes by several megabytes). To avoid this extra size, modern compilers and early mainframe debugging systems output the symbolic information into a separate file; for Microsoft compilers, this file is called a PDB file.

2
  • 6
    That is what I believed to be happening but this seems to just be false. I've gotten VS warnings saying PDBs cannot be found when linking to static libraries. Also, for exes/dlls VS produces 2 PDBs; I don't know what the seemingly meaningless one in the intermediate dir does but that's the only one produced for static libs, and as a different name than for exes/dlls.
    – David
    Sep 28, 2011 at 11:55
  • 4
    The only PDB produced for static libs (in the intermediate dir) is the debug information necessary when you link your dll/exe with this static lib. In case if you build both all of your static libs and your dll/exe on the same machine and don't move directories after build, these PDB are perfectly found and the debug information is included into exes/dlls. If you moved directories, or build static libs on another machine, VS fails to find the PDBs for static libraries and produces the warnings.
    – vond
    Nov 14, 2012 at 6:34
1

Weird behavior in VS2012. Building from scratch (or with /A option in nmake) will produce a .pdb file. Now delete the .lib and .pdb and rerun nmake (without /A of course, to run only link) and no .pdb file is output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.