相当于CMake中autoconf的AC_CHECK_DECLS

Equivalent to autoconf's AC_CHECK_DECLS in CMake

CMake 中 autoconf 的 AC_CHECK_DECLS 等同于什么?

我正在设置以下固定定义。我需要根据环境改变它。

target_compile_definitions(myapp PRIVATE HAVE_ENDIAN_H=1 HAVE_DECL_BSWAP_16=1 HAVE_DECL_HTOLE16=1 HAVE_DECL_BE16TOH=1 HAVE_DECL_LE16TOH=1 HAVE_DECL_HTOBE32=1 HAVE_DECL_HTOLE32=1 HAVE_DECL_BE32TOH=1 HAVE_DECL_LE32TOH=1 HAVE_DECL_HTOBE64=1 HAVE_DECL_HTOLE64=1 HAVE_DECL_BE64TOH=1 HAVE_DECL_LE64TOH=1 HAVE_DECL_HTOBE16=1)

我正在研究与 CMake 中的 autoconf AC_CHECK_DECLS 功能相同的功能。

手动

— Macro: AC_CHECK_DECLS (symbols, [action-if-found], [action-if-not-found], [includes = ‘AC_INCLUDES_DEFAULT’])
For each of the symbols (comma-separated list), define HAVE_DECL_symbol (in all capitals) to ‘1’ if symbol is declared, otherwise to ‘0’. If action-if-not-found is given, it is additional shell code to execute when one of the function declarations is needed, otherwise action-if-found is executed.

https://www.gnu.org/software/autoconf/manual/autoconf-2.65/html_node/Generic-Declarations.html

例子

AC_CHECK_DECLS([le16toh, le32toh, le64toh, htole16, htole32, htole64, be16toh, be32toh, be64toh, htobe16, htobe32, htobe64],,,
        [#if HAVE_ENDIAN_H
                 #include <endian.h>
                 #elif HAVE_SYS_ENDIAN_H
                 #include <sys/endian.h>
                 #endif])

更新 1

@Tsyvarev 给了我答案。非常感谢。

我更改了配置如下。有效。

include(CheckSymbolExists)
CHECK_INCLUDE_FILE(endian.h HAVE_ENDIAN_H)
CHECK_SYMBOL_EXISTS(htole16 "endian.h" HAVE_DECL_HTOLE16)
CHECK_SYMBOL_EXISTS(be16toh "endian.h" HAVE_DECL_BE16TOH)
CHECK_SYMBOL_EXISTS(le16toh "endian.h" HAVE_DECL_LE16TOH)
CHECK_SYMBOL_EXISTS(htobe32 "endian.h" HAVE_DECL_HTOBE32)
CHECK_SYMBOL_EXISTS(htole32 "endian.h" HAVE_DECL_HTOLE32)
CHECK_SYMBOL_EXISTS(be32toh "endian.h" HAVE_DECL_BE32TOH)
CHECK_SYMBOL_EXISTS(le32toh "endian.h" HAVE_DECL_LE32TOH)
CHECK_SYMBOL_EXISTS(htobe64 "endian.h" HAVE_DECL_HTOBE64)
CHECK_SYMBOL_EXISTS(htole64 "endian.h" HAVE_DECL_HTOLE64)
CHECK_SYMBOL_EXISTS(be64toh "endian.h" HAVE_DECL_BE64TOH)
CHECK_SYMBOL_EXISTS(le64toh "endian.h" HAVE_DECL_LE64TOH)
CHECK_SYMBOL_EXISTS(htobe16 "endian.h" HAVE_DECL_HTOBE16)
CHECK_SYMBOL_EXISTS(bswap_16 "byteswap.h" HAVE_DECL_HTOBE16)
target_compile_definitions(myapp PRIVATE HAVE_ENDIAN_H HAVE_DECL_BSWAP_16 HAVE_DECL_HTOLE16 HAVE_DECL_BE16TOH HAVE_DECL_LE16TOH HAVE_DECL_HTOBE32 HAVE_DECL_HTOLE32 HAVE_DECL_BE32TOH HAVE_DECL_LE32TOH HAVE_DECL_HTOBE64 HAVE_DECL_HTOLE64 HAVE_DECL_BE64TOH HAVE_DECL_LE64TOH HAVE_DECL_HTOBE16)

在 CMake 中,您可以检查符号是否使用 CheckSymbolExists 模块声明。示例:

include(CheckSymbolExists)
# ...
# Set HAVE_DECL_LE16TOH variable to 1 or 0 depending on declaration 'le16toh' symbol in 'endian.h' header.
CHECK_SYMBOL_EXISTS(le16toh "endian.h" HAVE_DECL_LE16TOH)

与 autotools 中的 AC_CHECK_DECLS 宏比较:

  • 调用 CHECK_SYMBOL_EXISTS() 一次检查单个符号。要检查多个符号,您可以循环调用它。

  • 对于 CHECK_SYMBOL_EXISTS(),您需要指定 具体 个 header 列表。

    如果 header 文件的包含依赖于其他宏,您需要先检查这个宏,或者再次调用 CHECK_SYMBOL_EXISTS()(宏是否被 定义), 或者使用普通命令 try_compile (check value of the macro). Alternatively, you may check whether specific header exists with CheckIncludeFile module.

  • 根据 CHECK_SYMBOL_EXISTS() 结果的附加操作可以在使用公共 if(<var>)if(NOT <var>).

    调用后发出