YAKOVM
YAKOVM

Reputation: 10153

Preprocessor in visual studio 2010-c++ project

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

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

Upvotes: 6

Views: 2305

Answers (3)

qdii
qdii

Reputation: 12973

A simple trick I always use is to redefine the macro at the line you want to check. 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 on GCC, but Visual Studio does the same thing.

Upvotes: 0

Steve
Steve

Reputation: 169

The best way to see exactly what the preprocessor is doing is to inspect its output directly. Intellisense is helpful but often does not match what the compiler understands.

Upvotes: 1

gwiazdorrr
gwiazdorrr

Reputation: 6339

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

  1. After each instance of #define SOME_MACRO put #error Defined here, then right click the source file and choose Compile. If compiler returns an error remove the directive that raises it and compile again. The last instance of this error will tail the definition visible in the source.

  2. Make each directive defining SOME_MACRO define it as something else and then, in the source file, add these lines after all includes:

    #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.

  3. Less intrusive way: put those 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 don't need the line number:

    #pragma message("Defining at " __FILE__)
    

    Compile the file. Looking at build log you should be able to tell the order of SOME_MACRO definitions.

Upvotes: 4

Related Questions