Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c - 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%0.2f
", 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?

question from:https://stackoverflow.com/questions/66054850/why-is-my-pdb-file-missing-symbols-when-i-link-object-files

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...