Preprocessor in Visual Studio 2010-C ++ project

In the .cpp file, I use the mmData1 macro. I searched in the project and see that this macro is defined in several files (i.e. there are several .h files that have the line #define mmData1 )

I want to know if in VS10 there is an opportunity to check from which file the preprocessor takes a macro

+6
source share
3 answers

If Intellisense does not know, then there is no direct way. However, there are indirect ways. Say your macro name is SOME_MACRO

  • After each instance of #define SOME_MACRO place #error Defined here , then right-click the source file and select Compile . If the compiler returns an error, remove the directive that will raise it and compile again. The final instance of this error will depend on the definition visible in the source.

  • Make each directive defining SOME_MACRO , define it as something else, and then add the following lines in the source file:

     #define STRINGIZE(x) STRINGIZE2(x) #define STRINGIZE2(x) #x #pragma message("SOME_MACRO is expanded as: " STRINGIZE(SOME_MACRO)) 

    Compile the source file; You should see the value in the build log.

  • Less intrusive way: put these lines after each #define SOME_MACRO

     #pragma push_macro("STRINGIZE") #pragma push_macro("STRINGIZE2") #define STRINGIZE(x) STRINGIZE2(x) #define STRINGIZE2(x) #x #pragma message("Defining at " __FILE__ ":" STRINGIZE(__LINE__)) #pragma pop_macro("STRINGIZE") #pragma pop_macro("STRINGIZE2") 

    Or if you do not need a line number:

     #pragma message("Defining at " __FILE__) 

    Compile the file. If you look in the build log, you should specify the order in which SOME_MACRO defined.

+4
source

The best way to see what the preprocessor is doing is to check its output directly. Intellisense is useful, but often does not match what the compiler understands.

+1
source

The simple trick that I always use is to override the macro on the line you want to test. When you compile the code, the preprocessor will complain and tell you where the previous definition was.

Example:

test.cpp contains:

 #include "test.h" int main() { #define SOMEMACRO 1 return 0; } 

test.h contains:

 #define SOMEMACRO 2 int foo(); 

when compiling test.cpp, I get this error message:

 test.cpp:5:0: warning: "SOMEMACRO" redefined [enabled by default] In file included from test.cpp:1:0: test.h:1:0: note: this is the location of the previous definition 

I tested GCC, but Visual Studio does the same.

0
source

Source: https://habr.com/ru/post/921719/


All Articles