Static library debug symbols

C++Visual StudioVisual Studio-2010

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.
  1. 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.
  1. 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.

Solution 3 - C++

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

Solution 4 - C++

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.

Solution 5 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - C++mloarView Answer on Stackoverflow
Solution 2 - C++bunkerdiveView Answer on Stackoverflow
Solution 3 - C++MilesDavies192View Answer on Stackoverflow
Solution 4 - C++PubbyView Answer on Stackoverflow
Solution 5 - C++nothrowView Answer on Stackoverflow