Macro definitions for headers, where to put them?

2019-04-04 09:20发布

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.

8条回答
在下西门庆
2楼-- · 2019-04-04 09:49

I would always put them on the command line via CPPFLAGS for the whole project. If you put them any other place, there's a danger that you might forget to copy them into a new source file or include a system header before including the project header that defines them, and this could lead to extremely nasty bugs (like one file declaring a legacy 32-bit struct stat and passing its address to a function in another file which expects a 64-bit struct stat).

BTW, it's really ridiculous that _FILE_OFFSET_BITS=64 still isn't the default on glibc.

查看更多
一纸荒年 Trace。
3楼-- · 2019-04-04 09:57

Well, it depends.

Most, I'd define via the command line - in a Makefile or whatever build system you use.

As for _FILE_OFFSET_BITS I really wouldn't define it explicitly, but rather use getconf LFS_CFLAGS and getconf LFS_LDFLAGS.

查看更多
登录 后发表回答