如何在预处理程序#if条件中捕获未定义的宏?

编程入门 行业动态 更新时间:2024-10-24 22:25:59
本文介绍了如何在预处理程序#if条件中捕获未定义的宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

基于此问题如何捕获空的已定义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条件中捕获未定义的宏?

本文发布于:2023-11-11 10:46:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578204.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   未定义   程序   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!