基于此问题如何捕获空的已定义gcc宏?我还有另一个问题.如何在预处理器 #if 条件下捕获未定义的宏?示例代码:
Based on this question How to catch empty defined macro with gcc? I have another problem. How to catch undefined macro in preprocessor #if condition? Example code:
#include <stdio.h> int main() { #if ENABLE_SOMETHING == 1 ... do something .. #endif return 0; }当未使用gcc编译器设置了 ENABLE_SOMETHING 时(是否有些标志),是否存在一种捕获错误/警告的方法?或者也许我可以使用外部工具?
Is there a way to catch error/warning when ENABLE_SOMETHING is not set using gcc compiler(maybe some flag)? Or maybe there are external tools which I can use?
我知道我可以写这样的东西:
I know than i can write something like this :
#ifndef ENABLE_SOMETHING #error "ENABLE_SOMETHING undefined!" #endif但是我在代码中有很多不同的定义( ENABLE_STH1 , ENABLE_STH2 , ENALBE_STH3 ...等),我没有不想手动解决此问题.我正在为我们的项目寻找一些自动解决方案.
But I have a huge amount of different defines(ENABLE_STH1, ENABLE_STH2, ENALBE_STH3 ... etc.) in code and i don't want to fix this manually. I'm looking for some automatic solution for our project.
推荐答案
当未设置ENABLE_SOMETHING时是否有一种捕获错误/警告的方法使用gcc编译器(也许有一些标志)?
Is there a way to catch error/warning when ENABLE_SOMETHING is not set using gcc compiler(maybe some flag)?
使用GCC,您可以使用 -Wundef 标志.
With GCC you can use the -Wundef flag.
摘自官方文档
-Wundef
-Wundef
警告是否在#if指令中评估未定义的标识符.此类标识符将替换为零.
Warn if an undefined identifier is evaluated in an #if directive. Such identifiers are replaced with zero.
例如,此C代码:
#include <stdio.h> int main(void) { #if UNDEFINED_MACRO printf("hi mum!\n"); #endif return 0; }...使用GCC编译,-Wundef标志产生以下结果:
... compiled with GCC and the -Wundef flag yields this:
$ gcc undef.c -Wundef undef.c: In function 'main': undef.c:5:5: warning: "UNDEFINED_MACRO" is not defined [-Wundef] #if UNDEFINED_MACRO ^更多推荐
如何在预处理程序#if条件中捕获未定义的宏?
发布评论