【问题标题】:Determine which preprocessor defines are used by included library headers确定包含的库头文件使用哪些预处理器定义
【发布时间】:2018-10-13 13:01:00
【问题描述】:

我正在查看一个项目,它有许多由 autotools 在config.h 中生成的预处理器定义。

现在,其中一些来自早期版本的源代码,也来自已被删减且源文件不再存在的部分内容。所以 - 该文件中的一些现在没用了,不应该生成。问题是 - 哪一部分?

您可以做的一件事是在当前项目源中搜索预处理器定义的使用。我已经做到了。但是 - 有一些定义会在包含时影响其他库的标头,例如 _GNU_SOURCE

我的问题:我如何确定哪些定义(包括当前在 config.h 中被注释掉的定义)在源中包含的标头上具有潜力?

(当然,在这里我需要精确而不是召回,因为无用的#define 并不是那么糟糕,但我确实需要避免缺少定义。)

【问题讨论】:

  • 有趣的问题,但我怀疑是否有令人满意的解决方案。至少当特性测试宏出现时,我想不出一种方法来知道它们是由静态分析定义的。
  • 这可能很大程度上取决于您的编译器。对于gcc,您可以使用-E -dM(而不是-c)编译您的代码,并列出预处理器知道的所有定义。
  • @JensGustedt:那对我有什么帮助?
  • @FelixPalmen:不完美的启发式算法仍然会有很大帮助。
  • 我建议不要理会功能测试宏,并限制自己清理宏,您可以通过检查项目自己的源来检查它们的必要性。或者至少从那开始。您可以将其作为一个长期项目来处理功能测试宏。

标签: c build c-preprocessor autotools configure


【解决方案1】:

最简单的方法是让autoscan 创建您的 configure.scan 文件,然后您可以剪切-n-粘贴您需要的内容。

$ cd myCproject
$ autoscan
aclocal.m4:16: warning: this file was generated for autoconf 2.63.
You have another version of autoconf.  It may work, but is not guaranteed to.
If you have problems, you may need to regenerate the build system entirely.
To do so, use the procedure documented by the package, typically `autoreconf'.
configure.ac:6: warning: AC_LANG_CONFTEST: no AC_LANG_SOURCE call detected in body
../../lib/autoconf/lang.m4:193: AC_LANG_CONFTEST is expanded from...
../../lib/autoconf/general.m4:2672: _AC_LINK_IFELSE is expanded from...
../../lib/autoconf/general.m4:2689: AC_LINK_IFELSE is expanded from...
aclocal.m4:1037: _LT_SYS_MODULE_PATH_AIX is expanded from...
aclocal.m4:4176: _LT_LINKER_SHLIBS is expanded from...
aclocal.m4:5251: _LT_LANG_C_CONFIG is expanded from...
aclocal.m4:159: _LT_SETUP is expanded from...
aclocal.m4:88: LT_INIT is expanded from...
configure.ac:6: the top level
configure.ac:6: warning: AC_LANG_CONFTEST: no AC_LANG_SOURCE call detected in body
../../lib/autoconf/lang.m4:193: AC_LANG_CONFTEST is expanded from...
../../lib/autoconf/general.m4:2672: _AC_LINK_IFELSE is expanded from...
../../lib/autoconf/general.m4:2689: AC_LINK_IFELSE is expanded from...
aclocal.m4:4176: _LT_LINKER_SHLIBS is expanded from...
aclocal.m4:5251: _LT_LANG_C_CONFIG is expanded from...
aclocal.m4:159: _LT_SETUP is expanded from...
aclocal.m4:88: LT_INIT is expanded from...
configure.ac:6: the top level
configure.ac: warning: missing AC_CHECK_FUNCS([clock_gettime]) wanted by: acs.c:405
configure.ac: warning: missing AC_CHECK_FUNCS([gethrtime]) wanted by: atomicclock.c:52
configure.ac: warning: missing AC_CHECK_FUNCS([gettimeofday]) wanted by: atomicclock.c:99
configure.ac: warning: missing AC_CHECK_HEADERS([mach/mach.h]) wanted by: atomicclock.c:16
configure.ac: warning: missing AC_CHECK_HEADERS([sys/time.h]) wanted by: atomicclock.c:13
configure.ac: warning: missing AC_PROG_CXX wanted by: ltmain.sh:677
configure.ac: warning: missing AC_PROG_RANLIB wanted by: ltmain.sh:1601
configure.ac: warning: missing AC_TYPE_UINT64_T wanted by: acs.c:338
$

完成时,configure.scan 文件被创建。我个人项目的一个实际例子如下所示:

AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB

# Checks for libraries.
# FIXME: Replace `main' with a function in `-lrt':
AC_CHECK_LIB([rt], [main])

# Checks for header files.
AC_CHECK_HEADERS([mach/mach.h stdint.h stdlib.h string.h sys/time.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T

# Checks for library functions.
AC_CHECK_FUNCS([clock_gettime gethrtime gettimeofday])

AC_CONFIG_FILES([Makefile])
AC_CONFIG_SUBDIRS([automake-1.15])
AC_OUTPUT

您可以从configure.scan 中剪切并粘贴您需要的内容,然后将其插入到您自己的configure.am(或configure.in)文件中相应部分的下方。

autoscan 实用程序是 autoconf 包的一部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    相关资源
    最近更新 更多