VC 重复宏定义 Warning

如果我们在代码中重复定义一个宏(macro redefine),例如:

#define SUM(a,b) a+b
#define SUM(a,b) a*b

会遇到下面的错误提示:

MRedef.c(20): error C2220: warning treated as error - no 'object' file generated
MRedef.c(20): warning C4005: 'SUM': macro redefinition
MRedef.c(19): note: see previous definition of 'SUM'

解决方法是在文件头部加入 Disable 这个 Warning 的指令如下:

#pragma warning (disable : 4005)

完整的代码如下:

#include  <Uefi.h>
#include  <Library/UefiLib.h>
#include  <Library/ShellCEntryLib.h>

#pragma warning (disable : 4005)

/*
MRedef.c(20): error C2220: warning treated as error - no 'object' file generated
MRedef.c(20): warning C4005: 'SUM': macro redefinition
MRedef.c(19): note: see previous definition of 'SUM'
*/

#define SUM(a,b) a+b
#define SUM(a,b) a*b


/***
  Print a welcoming message.

  Establishes the main structure of the application.

  @retval  0         The application exited normally.
  @retval  Other     An error occurred.
***/
INTN
EFIAPI
ShellAppMain (
  IN UINTN Argc,
  IN CHAR16 **Argv
  )
{
        int c =3,d=4;

  Print(L"Hello there fellow Programmer.\n");
  Print(L"Welcome to the world of EDK II.\n");

  Print(L"Macro test %d\n",SUM(c,d));
  
  return(0);
}

参考:

1.https://stackoverflow.com/questions/25201724/fix-macro-redefinition-in-c

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注