I am analyzing a legacy code which heavily using macro, I am lost in understanding how macro are expanding in code.
Could any one suggest me some tool or technique so that I can study actual code generated from macro expansion.
Platform : Windows XP
Language : C++
Compiler : VC6
To run the GCC as a pre-processor only do:
gcc -E source-file.cc > processed-source-file.cc
It also do all the #includes that you may or may not want.
With visual studio you can use the Generate Preprocessed File option.
In the properties for your project select
C/C++/Preprocessor/
In that tab there is an option to:
generate a preprocessed file.
Select Yes, with numbers.
Run the pre-processor (cpp) on the source file.
Since you're using Visual C this doesn't help you, but it might be helpful to others to mention:
Netbeans 6.7 is able to display the macro expanded version of C/C++ code in a separate window during editing.
Related
The C code that I am working on has lots of macros everywhere. I need to see expanded states of the macros to decrease my coding time.
1) What is the best way of code development considering codes with many macros? For example, there is variable which is probably declared somewhere in the macros and I have to search all macros to find its type.
2) Although using ctags in VIM helps me to jump to the macro definition, is there a VIM plugin that recursively expands and shows C/C++ macros in a C/C++ file while I am editing the file?
P.S. There is a similar question here but its answers are not related with the question:
vim plugin for exploring macros written by c/c++
P.P.S. When I use the solution at Seeing expanded C macros, I got a huge file that contains all other include files and so on. Also the whole file cannot be preprocessed because I have to change the cmake build system.
I know that you can provide a command line or add switches in your project properties but can you do that from source file? I need to set certain switches for certain source files without going into project properties and manually doing it everytime. So maybe you can do it with preprocessors from C++ source file?
There're some #pragma's that can, for example, alter code generation/optimization for a block of code, but I think that's pretty limited thing.
First: Compilation/linking do not execute your C++ code. So you can't write a C++ piece'o code and make decision (to change some project settings of THIS project) based on some C++ conditions (like if/else/...).
#pragma and #defines are pre-processor directives as you already know. Though compiler understands #pragmas, programming capability is limited and usually compiler specific.
A common use of these is to make the code platform independent.
To answer your original question "how do I modify the project properties progrmatically (not manuall)?": You can write "code" using #pragmas & #defines (even conditional ones) in your code to control various settings. Usually support is compiler-vendor specific, so your code will most probably not be portable (across compilers) unless you're careful.
On my GCC, I always found c++config.h a good example of various cpp directive. On my windows PC it's installed in X:\MinGW\lib\gcc\mingw32\4.5.2\include\c++\mingw32\bits\c++config.h
To find out supported cpp directives, RTM of your compiler.
The other option obviously is to write shell/perl/... script to make modifications to the project settings/Makefiles. I recommend that compared to getting yourself tangled in cpp directives. :)
Of course this option would change the settings before compilation and not during. But if that's not a prob for you, go with this rather than cpp directives.
As has been mentioned, #pragma statements are very vendor specific. For Visual Studio 2005, take a look here for the supported #pragma statements: Pragma Directives
I would also with thekashyap regarding writing a script to modify the project settings for each file for the following reasons:
In the event that you have to add or change a setting, there is a *single* place to modify.
Because there is only one place to look, future maintainers know where to find special settings for a given file. They won't be surprised by something set manually that is hidden deep within the project settings dialogs.
My problem:
I have created a C project in Visual Studio 2010 and defined a conditional compilation constant named QWERTY. My C source code executes #ifdef QWERTY in order to see if it's defined or not.
When I add /DQWERTY into the command line additional options of my C project in the IDE, then the constant is correctly detected after compilation, just as expected.
So, my questions is: how can I define that constant from the command line using MSBuild?
Just for clarity reasons, I'm running this command:
MSBuild myproj.sln
Please don't propose to use the switch /p:DefineConstants; I've created a C project, not a C# project, and that switch is useless for C/C++ projects.
As you've probably already understood, I'm feeling more and more desperate about this trivial problem. Please enlighten me and show me that there is a simple solution around the corner that I've missed of pure stupidity.
My project has a bunch of #ifdefs. The macros used by these #ifdefs are usually passed through the command line using the '/D' option to get different build configurations. Visual studio incorrectly assumes that these macros are not defined and greys out the code blocks present inside these #ifdefs. The problem is not syntax highlighting - I can turn the grayed out code to colored code from Options; the main issue is that I am not able to go to the function definition of any functions present inside that #ifdef. I tried reading about Visual Studio hint files but that didn't work for me.
Can anyone help me with this issue? I am using Visual Studio 2008.
Did you define several kinds of builds within VS as Configurations like Debug, Release, or are you building with makefiles? If you haven't taught VS about your /D options then I guess it can't help you. But you should be able to set up Preprocessor Definitions under project properties (Configuration Properties, C/C++, Preprocessor) to get the effect you want, right?
For each option /DMACRO=XXX that you pass to the compiler, specify MACRO=XXX in the IntelliSense Preprocessor Definitions. For each option /DMACRO (no value) that you pass to the compiler, specify MACRO in the IntelliSense preprocessor definitions.
If you're not debugging, and you're just trying to get intellisense or whatever to react, you can always just throw up a quick #define to force the IDE to behave.
Seems you have a similar problem as this fellow:
Can intellisense be enabled in VS2008 within preprocessor directive blocks like #ifndef ... #endif
I'd like to see all the asm produced by Visual Studio C++ to learn a bit about ASM, compilers, etc. I know with GCC, you can do it with the -S argument, but not in VS. How do I go about doing this?
The easiest way to do this is to start your program in the integrated debugger, then turn on the assembly language view. This shows the assembly language interleaved with your original source code.
Back when I used VS, this was Alt+F7 or something but it's probably changed. The advantage of doing this (over using -S equivalent) is you can focus on exactly which line(s) of code you're interested in.
Right-click on your project and then Properties -> C/C++ -> Output files -> Assembler Output and set it to something else than No Listing. Equivalently, you can add one of the /FA switches to the compiler's command line.