uv_
uv_

Reputation: 773

Find place where some macro was #define-ed

The bottom line: building with VS2012, there's a macro (WIN32_LEAN_AND_MEAN) defined in my project I can't find #define-ed anywhere: not in C/C++ --> Preprocessor, not inherited from parent or project dependencies (microsoft.cpp.props), not in the command-line. It is not mentioned anywhere in the vcxproj.

Compiling a single source/header in the project, I find it is defined already on the first line of the header. Putting this at the top of my header:

#pragma once

#ifndef WIN32_LEAN_AND_MEAN
#pragma  message ("WIN32_LEAN_AND_MEAN not defined")
#else
#pragma  message ("WIN32_LEAN_AND_MEAN defined")
#endif
/* ... */

Printed "WIN32_LEAN_AND_MEAN defined" to the build output console.

According to suggestions that were posted in another - very similar - question, I tried to re-define the macro:

#define WIN32_LEAN_AND_MEAN 123

#ifndef WIN32_LEAN_AND_MEAN
#pragma  message ("WIN32_LEAN_AND_MEAN not defined")
#else
#pragma  message ("WIN32_LEAN_AND_MEAN defined")
#endif

and obviously get a build warning:

C:\sys\inc\myproj\myproj_someheader.h(5): warning C4005: 'WIN32_LEAN_AND_MEAN' : macro redefinition

command-line arguments : see previous definition of 'WIN32_LEAN_AND_MEAN'

However, as previously mentioned, not in that project configuration (vcxproj), and also not in any common properties, is that macro currently defined.

My question is: How can I find where that macro actually came from?

Upvotes: 1

Views: 604

Answers (2)

uv_
uv_

Reputation: 773

After looking at the project properties (Project Properties->C/C++->Preprocessor->Preprocessor Definitions and at C/C++->Command Line), the vcxproj itself (looked if there are Preprocessor Definitions configured for the specific source file), common properties (configured in Microsoft.Cpp.Props), headers and source-code, nowhere where WIN32_LEAN_AND_MEAN was defined, it turns out it was defined by the %CL% environment variable.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121649

There is no #define WIN32_LEAN_AND_MEAN anywhere on your development system.

There are plenty of #ifdef WIN32_LEAN_AND_MEAN (or equivalent).

The point is that you define it, if you need it/want it.

http://web.archive.org/web/20121219084749/http://support.microsoft.com/kb/166474

VC_EXTRALEAN and WIN32_LEAN_AND_MEAN are used to exclude rarely-used services from Windows headers. VC_EXTRALEAN can only be used in MFC projects, but WIN32_LEAN_AND_MEAN can be used in any project.

If you want to see how macros are being expanded for your particular project, you can use the /P switch, and look at the corresponding .i file(s):

https://social.msdn.microsoft.com/Forums/vstudio/en-US/162e8850-f442-4283-a419-6c328684388e/showing-how-a-macro-is-expanded?forum=vclanguage

The C++ compiler has a /P switch which means pre-process to a file.

You can enable this from Project -> Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocess to a File.

After this option is set, you will get a .i file for every .cpp file.

Be warned that these .i files are huge files.


ADDENDUM:

  1. I went into MSVS 2015 and created a quick'n'dirty test program:

    // TestWin32LeanAndMean.cpp : Defines the entry point for the console application.
    //    
    #include "stdafx.h"
    #include <iostream>
    
    #ifndef WIN32_LEAN_AND_MEAN
    #pragma  message ("WIN32_LEAN_AND_MEAN not defined")
    #else
    #pragma  message ("WIN32_LEAN_AND_MEAN defined")
    #endif
    
    int main()
    {
        std::cout << "Hello World" << std::endl;
        return 0;
    }
    

    Compiler log:

    1>------ Build started: Project: TestWin32LeanAndMean, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> TestWin32LeanAndMean.cpp 1> WIN32_LEAN_AND_MEAN not defined 1> TestWin32LeanAndMean.vcxproj -> D:\paul\proj\msvs_2015\TestWin32LeanAndMean\Debug\TestWin32LeanAndMean.exe ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

  2. I then went into Project > Properties > Preprocessor, and added "WIN32_LEAN_AND_MEAN":

    MSVS Preprocessor Definitions:

    _DEBUG;_CONSOLE;%(PreprocessorDefinitions);WIN32_LEAN_AND_MEAN

    The build still said "undefined" (?!?)

    1>------ Build started: Project: TestWin32LeanAndMean, Configuration: Debug Win32 ------ 1> stdafx.cpp 1> TestWin32LeanAndMean.cpp 1> WIN32_LEAN_AND_MEAN not defined 1> TestWin32LeanAndMean.vcxproj -> D:\paul\proj\msvs_2015\TestWin32LeanAndMean\Debug\TestWin32LeanAndMean.exe

  3. So I manually edited my .vcsproj file and rebuilt:

    TestWin32LeanAndMean.vcxproj:

    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
      <ClCompile>
        <PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        ...
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
      <ClCompile>
        <PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        ...
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
      <ClCompile>
        <PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
        ...
    <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
      <ClCompile>
        <PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
      ...
    

    I finally got WIN32_LEAN_AND_MEAN" defined:

    1>------ Rebuild All started: Project: TestWin32LeanAndMean, Configuration: Debug Win32 ------
    1>  stdafx.cpp
    1>  TestWin32LeanAndMean.cpp
    1>  WIN32_LEAN_AND_MEAN defined
    1>  TestWin32LeanAndMean.vcxproj -> D:\paul\proj\msvs_2015      \TestWin32LeanAndMean\Debug\TestWin32LeanAndMean.exe
    
  4. To see exactly what the MSVS macro preprocessor is doing, you should be able to use "/P" ("Preprocess to a file").

  5. In my experience - and in the test above - "WIN32_LEAN_AND_MEAN" is typically NOT DEFINED in the environment.

  6. If it IS somehow defined, the places to look would be:

    a) MSVS > Project > Properties

    b) Windows Explorer > Project > .vcxproj file

    c) MSVS Install folder > Templates > ProjectTemplates

Upvotes: 3

Related Questions