gpt4 book ai didi

c++ - 找到一些宏被#define-ed的地方

转载 作者:可可西里 更新时间:2023-11-01 11:15:29 28 4
gpt4 key购买 nike

底线:使用 VS2012 构建,在我的项目中定义了一个宏 (WIN32_LEAN_AND_MEAN) 我在任何地方都找不到#define-ed:不在 C/C++ 中 --> 预处理器,不是从父级或项目依赖项继承的 ( microsoft.cpp.props),而不是在命令行中。 vcxproj 中的任何地方都没有提到它。

编译项目中的单个源/ header ,我发现它已经在 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
/* ... */

将“WIN32_LEAN_AND_MEAN defined”打印到构建输出控制台。

根据发布的建议in another - very similar - question ,我试图重新定义宏:

#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

显然会收到构建警告:

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'

但是,如前所述,当前定义的宏不在该项目配置 (vcxproj) 中,也不在任何公共(public)属性中。

我的问题是:我怎样才能找到那个宏的实际来源?

最佳答案

在您的开发系统上的任何地方都没有#define WIN32_LEAN_AND_MEAN

有很多#ifdef WIN32_LEAN_AND_MEAN(或等价物)。

关键是定义它,如果您需要它/想要它。

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.

如果您想查看宏是如何为您的特定项目扩展的,您可以使用 /P 开关,并查看相应的 .i 文件:

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.


附录:

  1. 我进入 MSVS 2015 并创建了一个 quick'n'dirty 测试程序:

    // 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;
    }

    编译日志:

    1>------ 构建开始:项目:TestWin32LeanAndMean,配置:调试 Win32 ------1> stdafx.cpp1> TestWin32LeanAndMean.cpp1> WIN32_LEAN_AND_MEAN 未定义1> TestWin32LeanAndMean.vcxproj -> D:\paul\proj\msvs_2015\TestWin32LeanAndMean\Debug\TestWin32LeanAndMean.exe========== 构建:1 次成功,0 次失败,0 次更新,0 次跳过 ==========

  2. 然后我进入项目 > 属性 > 预处理器,并添加“WIN32_LEAN_AND_MEAN”:

    MSVS 预处理器定义:

    _DEBUG;_CONSOLE;%(PreprocessorDefinitions);WIN32_LEAN_AND_MEAN

    构建仍然说“未定义”(?!?)

    1>------ 构建开始:项目:TestWin32LeanAndMean,配置:调试 Win32 ------ 1> stdafx.cpp 1> TestWin32LeanAndMean.cpp 1> WIN32_LEAN_AND_MEAN 未定义 1> TestWin32LeanAndMean.vcxproj -> D:\paul\proj\msvs_2015\TestWin32LeanAndMean\Debug\TestWin32LeanAndMean.exe

  3. 所以我手动编辑了我的 .vcsproj 文件并重建了:

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

    我终于得到了 WIN32_LEAN_AND_MEAN"的定义:

    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. 要准确查看 MSVS 宏预处理器在做什么,您应该能够使用“/P”(“对文件进行预处理”)。

  5. 根据我的经验 - 在上面的测试中 - “WIN32_LEAN_AND_MEAN”在环境中通常NOT DEFINED

  6. 如果它以某种方式定义的,那么要查看的地方将是:

    a) MSVS > 项目 > 属性

    b) Windows 资源管理器 > 项目 > .vcxproj 文件

    c) MSVS 安装文件夹 > 模板 > 项目模板

关于c++ - 找到一些宏被#define-ed的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54188263/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com