gcc - msvc 和 gcc 上的用户警告?

标签 gcc visual-c++ user-warning

在 MSVC 中,我在标题中有这个:

#define STR(x)          #x
#define STR2(x)         STR(x)
#define NOTE(text)      message (__FILE__ "(" STR2(__LINE__) ") : -NOTE- " #text)
#define noteMacro(text) message (__FILE__ "(" STR2(__LINE__) ") : " STR2(text))

我也是

#pragma NOTE(my warning here)

海湾合作委员会有:

#warning(my warning here)

但是 MSVC (2003) 在看到 #warning 并给出“fatal error C1021: invalid preprocessor command 'warning'”时抛出异常

我该怎么办?有没有办法让 GCC 识别 MSVC 警告或 MSVC 不对 GCC 警告抛出错误?有什么我可以做的对两者都有效的事情吗?我可以让 GCC 警告我未知的编译指示,但这不是最理想的解决方案。

最佳答案

我找到的针对此问题的最佳解决方案是在通用 header 中包含以下内容:

// compiler_warning.h
#define STRINGISE_IMPL(x) #x
#define STRINGISE(x) STRINGISE_IMPL(x)

// Use: #pragma message WARN("My message")
#if _MSC_VER
#   define FILE_LINE_LINK __FILE__ "(" STRINGISE(__LINE__) ") : "
#   define WARN(exp) (FILE_LINE_LINK "WARNING: " exp)
#else//__GNUC__ - may need other defines for different compilers
#   define WARN(exp) ("WARNING: " exp)
#endif

然后使用

#pragma message WARN("your warning message here")

贯穿整个代码而不是#warning

在 MSVC 下,您会收到如下消息:

c:\programming\some_file.cpp(3) : WARNING: your warning message here

在 gcc 下你会得到:

c:\programming\some_file.cpp:25: note: #pragma message: WARNING: your warning message here

不完美,但合理的折衷。

关于gcc - msvc 和 gcc 上的用户警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740684/

相关文章:

gcc arm -- 确保使用内联 asm 语句内联函数时保留参数

gcc - 如何在 Mac OS X 上使用 AVX/pclmulqdq

c - 如何在 Linux 上为 gcc 制作一个简单的 makefile

c++ - 直接调用 WinAPI 的 Windows 服务的 Visual C++ 项目类型

c++ - 我可以在现代 Intel Core CPU 上测量分支预测失败吗?

c++ - Visual c++ 6.0 应用程序大小大于 visual studio 2010 应用程序大小

pymc3 - 用户警告 pymc3 : What does reparameterize mean?

python - 二阶 ODE dopri5 python UserWarning : larger nmax

c - 编译 gcc 和 clang 的不同结果