Charlie Gallagher
Charlie Gallagher

Reputation: 616

Why is my pdb file missing symbols when I link object files?

I'm compiling a very simple C program with two source files.

File 1:

#include <stdio.h>
#include "conv.h"

/* print Fahrenheit-Celsius table
  for fahr = 0, 20, ..., 300 */

int main()
  {
    int fahr;
    float celsius;
    int lower, upper, step;

    lower = 0;          /* lower limit of temperature table */
    upper = 300;        /* upper limit */
    step = 20;          /* step size */

    fahr = lower;
    while (fahr <= upper) {
      celsius = f2c(fahr);
      printf("%d\t%0.2f\n", fahr, celsius);
      fahr = fahr + step;
    }
    return 0;
  }

File 2:

#include "conv.h"

/* Convert Fahrenheit to Celsius */
float f2c(int fahr) {
    float celsius;
    celsius =  (fahr-32) * (5.0 / 9.0);
    return celsius;
}

/* Convert Celsius to Fahrenheit */
float c2f(int cel) {
    float fahr;
    fahr = cel * (9.0 / 5.0) + 32;
    return fahr;
}

I'm 'debugging' the program in windbg, although the program runs fine -- it's for experimentation. I'm compiling using the command-line Developer Command Prompt for VS 2019 in two ways. In the first case, I run the whole thing through CL:

cl /Zi /Fd:fahr fahr.c conv.c

and in the second case, I'm compiling without linking, then linking the object files:

cl /c fahr.c conv.c
cl /Zi /Fd:fahr fahr.obj conv.obj

In the first case, I can debug in windbg just fine. I open the executable, set a breakpoint, run it, and up comes my source file. But when I compile the second way, windbg will no longer connect to my source, and it fails to find variables in fahr.c and conv.c. As far as I can tell, everything is the same except for the circumstances when I link. What am I missing?

Upvotes: 0

Views: 393

Answers (1)

Charlie Gallagher
Charlie Gallagher

Reputation: 616

The debug file (.pdb) is added to in stages as each part of the program is built. When you compile files, the compiler adds their symbols to the pdb file (if you ask it to). Then, when you link files, it adds further symbols to the pdb file. I wasn't getting source file symbols because I didn't generate any debug data when I compiled the object files. The fix is:

cl /c /Zi /Fd:fahr fahr.c conv.c
cl /Zi /Fd:fahr fahr.obj conv.obj

My mistake was that I thought the debug file was generated by the linker only, which never sees the source files. Rather both the compiler and the linker have a role in producing the debug file.

Upvotes: 1

Related Questions