I have a macro that is dynamically generated before compilation (it's supposed to contain the build number). However I think there's some error with the way it's generated so I would like to check the content of this macro. How can I do that?
I'm using the MSVC2008 compiler.
You can use /p option and write preprocessor output to a file. Or
You can use /E option and write preprocessor output to stdout
You can set the "Preprocess to a file" option on the project or file's properties to true. That will let you see exactly what the preprocessor emits.
(It's under Configuration Properties → C/C++ → Preprocessor.)
Related
I am trying to use the OutDir Macro from the Project properties within my c++ file to build a path.
But I can't find a way to assign the OutDir content to a variable in my code.
I tried this:
#define OUTPUT_DIR $OutDir
I can't seem to use this correctly.
You can specify pre-processor definitions in the "Project Properties->C/C++->Preprocessor->Preprocessor Definitions" list as:
OUTPUT_DIR=$(OutDir)
and then you can use that macro in your source code. You may need to textify it first. i.e.
#define TEXTIFY(x) #x
then use it as
TEXTIFY(OUTPUT_DIR)
see this answer. Although looking at this answer, it is possible that VC++ 2017 has some issues with this.
I believe you can also add the quotes into the options itself which might be a way round it.
OUTPUT_DIR="$(OutDir)"
I have a C++ package that allows for different behavior if the user defines different pre-processor constants. For example, a user can use /D ALLOW_WIDE_CHAR in the makefile to change the behavior to support wide characters.
I would like to document these options with Doxygen, but Doxygen complains because there is no actual #define in the code. It is up to the user to do that.
Adding the definition to the PREDEFINED list in the configuration file doesn't help.
Suggestions?
One option I found is to create an extra source file, say Doxygen.h, that is not compiled, but is added to the Doxygen file list. This file can use #define to define those pre-processor constants. It seems like there should be a better solution, though.
Sometimes I run across quite complicated macros and I would like to view what they will expand to given the parameters (there are string concatenations in there as well). Is there by any chance a program out there that will expand the macros?
I am aware of the compiler flag -E but what about a single file (or preferably a single macro?)
Eclipse will expand macros if you mouse over them. For macros which include other macros, Eclipse can even step through the macro expansions one step at a time.
(You can use Eclipse for this even if you normally use another IDE.)
A few options are:
In GCC:
gcc -E filename.c
Using computers precompiler:
cpp filename.c
In visual Studio:
Right-click on the file on the Solution Explorer, goto Properties. Under Configuration Properties->C/C++->Preprocessor & "Generate Preprocessed File"
The C and C++ preprocessor is called cpp on most systems - you can use it directly:
cpp somefile.c
will preprocess somefile.c, expanding macros and write the results to standard output. If you are using the Microsoft compiler:
cl -E somefile.c
will do the same, assuming you have the compiler on your PATH.
Netbeans IDE allows you to view what a macro expands to by holding Ctrl+Alt and hovering/clicking a macro.
I have a project in C++ that I would like to view the preprocessor output to see what some #defines and macros would look like. I tried the /p switch to turn on the preprocess to a file option to the compiler (it turns off full compilation and only runs the preprocessor) but my project now refuses to compile and shows a long list of errors starting with:
"Cannot open include file: 'stdafx.h': No such file or directory".
The project compiles fine without the /p argument, of course. Any suggestions?
If you run cl.exe on its own then you would need to supply all the same parameters as the IDE does when building, otherwise it can't find all the include paths and preprocessor macros. However, there is another way to do this. In the project file, select the .cpp file you want, and choose Properties > C++ > Preprocessor > Generate preprocessor file. Then compile the .cpp file.
This will generate the preprocessed file (file.i I think) in the output directory. It's a shame there isn't an easier way of just selecting a file and hitting 'preprocess' but this could probably be done quite easily with a VisualStudio macro. Don't forget to set the option back afterwards.
Are all the other options (like /I) still the same when you compile with /p? It sounds like it's not picking up your header files. Alternately because it's trying to pre-process all the includes it's no longer generating the stdafx.h precompiled header - you could try just including all the needed headers directly instead of that.
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.
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.
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.
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.