页眉宏定义,放在哪里呢? [英] Macro definitions for headers, where to put them?

查看:168
本文介绍了页眉宏定义,放在哪里呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当定义了头靠宏,如 _FILE_OFFSET_BITS FUSE_USE_VERSION _GNU_SOURCE ,其中就是把它们<?最好的地方/ EM>

我考虑的一些可能性包括


  1. 在依赖于通过在头露出定义的源文件的顶部包含在文件

  2. 之前立即包括相关的头(S)

  3. 定义在通过编译器 CPPFLAGS 水平? (如 -D_FILE_OFFSET_BITS = 64 )为:

    1. 整个源回购

    2. 整个项目

    3. 只是需要它的源代码


  4. 在项目头,其中也应包括有关标头中的宏适用的

  5. 我没有想到的其他一些地方,但是是无限优越

一个提示:理由是由适用性进行,自动工具,等构建系统是我的决定的一个因素。


解决方案

如果宏影响系统的头,他们可能应该在那里的影响,包括那些系统头文件(包括那些包含他们间接的)每个源文件去的地方。因此,最符合逻辑的地方是在命令行中,假设你的构建系统允许您设置如CPPFLAGS影响到每一个文件的编译。

如果您使用precompiled头,并具有因此必须在每一个源文件中首次列入(如MSVC项目的stdafx.h)一precompiled头,那么你可以把他们在那里了。

有关影响自足库(无论是第三方或由您写的)宏,我会创建一个包装头,它定义宏,然后包括库的头。然后从项目库中的所有用途应该包括你的头的包装,而不是直接包括库的头。这样就避免了不必要的定义宏,并明确指出,它们与该库。如果有库之间的依赖关系,那么你可能想使宏全球(在构建系统或precompiled头)只是为了安全起见。

When defining macros that headers rely on, such as _FILE_OFFSET_BITS, FUSE_USE_VERSION, _GNU_SOURCE among others, where is the best place to put them?

Some possibilities I've considered include

  1. At the top of the any source files that rely on definitions exposed by headers included in that file
  2. Immediately before the include for the relevant header(s)
  3. Define at the CPPFLAGS level via the compiler? (such as -D_FILE_OFFSET_BITS=64) for the:

    1. Entire source repo
    2. The whole project
    3. Just the sources that require it

  4. In project headers, which should also include those relevant headers to which the macros apply
  5. Some other place I haven't thought of, but is infinitely superior

A note: Justification by applicability to make, autotools, and other build systems is a factor in my decision.

解决方案

If the macros affect system headers, they probably ought to go somewhere where they affect every source file that includes those system headers (which includes those that include them indirectly). The most logical place would therefore be on the command line, assuming your build system allows you to set e.g. CPPFLAGS to affect the compilation of every file.

If you use precompiled headers, and have a precompiled header that must therefore be included first in every source file (e.g. stdafx.h for MSVC projects) then you could put them in there too.

For macros that affect self-contained libraries (whether third-party or written by you), I would create a wrapper header that defines the macros and then includes the library header. All uses of the library from your project should then include your wrapper header rather than including the library header directly. This avoids defining macros unnecessarily, and makes it clear that they relate to that library. If there are dependencies between libraries then you might want to make the macros global (in the build system or precompiled header) just to be on the safe side.

这篇关于页眉宏定义,放在哪里呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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