需要澄清有关#ifndef的使用 [英] Clarification needed regarding the use of #ifndef

查看:98
本文介绍了需要澄清有关#ifndef的使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Ubuntu上阅读此代码:

I''m reading this code on Ubuntu:

/**
  add time header
*/
static void _add_time_header( struct evbuffer *buf )
{
        char date[50];
#ifndef WIN32
        struct tm cur;
#endif
        struct tm *cur_p;
        time_t t = time( NULL );
#ifdef WIN32
        cur_p = gmtime( &t );
#else
        gmtime_r( &t, &cur );
        cur_p = &cur;
#endif
        strftime( date, sizeof( date ), "%a, %d %b %Y %H:%M:%S GMT", cur_p);
        evbuffer_add_printf( buf, "Date: %s\r\n", date );
}



我不明白为什么编码人员使用 #ifndef WIN32
我已经阅读了有关gmtimegmtime_r的手册,但找不到任何内容.
请说明我为什么?

谢谢



I don''t understand why the coder use #ifndef WIN32 ,
I have read the manual about gmtime and gmtime_r, but I couldn''t find anything.
Please clarify me why?

Thank you

推荐答案

#ifndef是对部分编译器(称为预处理器)的指令,仅在符号(在这种情况下为"WIN32")中包含代码) 没有定义.同样,如果定义了符号,#ifdef包括代码.

它所要做的就是使一个代码文件可以应付不同的编译环境.

最常见的用途可能是
#ifndef is an instruction to part of the compiler (called the preprocessor) to only include the code if a symbol (in this case "WIN32") is not defined. Equally, #ifdef includes the code if the symbol is defined.

All it does is makes it possible to have one code file which can cope with different compilation circumstances.

The most common use is probably
#ifdef DEBUG

仅包括跟踪或记录语句(如果这是测试版本而不是发行版).这样,当出现问题时,将在插入跟踪代码的情况下测试相同的代码,而无需更改源代码.

在显示的代码片段中,#ifddef WIN32用来使代码在两种不同的系统时间格式下工作相同:Ubuntu和Windows(它们的处理方式略有不同).

to only include tracing or logging statements if this is the test version rather than release. That way, when there is a problem, the same code gets tested with tracing code inserted, but without changing the source.

In the code fragment you show, #ifddef WIN32is there to let the code work the same with two different systems Time formats: Ubuntu and Windows (which handle it a little differently).


这篇关于需要澄清有关#ifndef的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆