How to figure out what value MSVC is using for a p

2019-07-15 12:50发布

问题:

I'm attempting to use a /D compiler option on MSVC6 to define a string, but there's something weird about using double quotes around it. To debug this problem, it would be extremely helpful for me to be able to see what value the preprocessor is actually substituting into my code where the macro is expanded. Is there any way I can do this? I tried creating a Listing file with "assembly and source", but the source contains the original macro name and the ASM is some incomprehensible gibberish at that line. Is there a way to get the macro value at compile time?

Failing that (or perhaps more useful), how do I specify a string with the /D option? It needs to substitute into my source with double quotes around it, since I'm using it as a string literal.

回答1:

Try one of the following options to CL.exe:

/E preprocess to stdout
/P preprocess to file

If you're building within Visual Studio, you can specify custom command-line options in one of the project property dialogs.



回答2:

MSVC has a compiler flag that allows you to see the preprocessed source file with all the macros expanded, comments removed, etc. - the entire translation unit in terms of the actual code that will compile. Preprocessed output should give you the insight you're looking for regarding your macro expansion. More info here.



回答3:

There's an option to pass to the compiler (/P) and it will write the preprocessor output into my_cpp_file.i where you can look at it.