跨平台的C / C ++程序通常会使用大量的宏来测试构建该平台的平台中是否存在某些功能:
...
#if defined(HAVE_EPOLL)
handle_connection_with_epoll()
#else
handle_connection_without_epoll()
#endif // HAVE EPOLL
...
通常,然后将使用./configure
生成一个config.h
头文件,如果存在epoll接口(即Linux),则将在其中定义HAVE_EPOLL
,我们将使用handle_connection_with_epoll()
代码,否则将不会定义HAVE_EPOLL
,我们将获得handle_connection_without_epoll()
代码。
读取这类代码很痛苦,尤其是在存在嵌套条件宏分支的情况下:
...
#if defined(HAVE_EPOLL)
#if defined(HAVE_FCNTL)
handle_connection_with_epoll_and_fcntl()
#else
handle_connection_with_epoll()
#endif // HAVE_FCNTL
#else
#if defined(HAVE_FCNTL)
handle_connection_without_epoll()
#else
...
#endif // HAVE_FCNTL
#endif // HAVE EPOLL
...
因此,在./configure
之后是否有任何工具可用于删除那些无效的宏分支,以便使代码更清晰地阅读?
例如,如果在HAVE_EPOLL
之后定义了HAVE_FCNTL
和./configure
,则上面的代码段将变为
handle_connection_with_epoll_and_fcntl()
这更清楚了!