相当于CMake中的autoconf的AC_CHECK_DECLS

时间:2017-06-26 07:03:23

标签: c++ 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)

1 个答案:

答案 0 :(得分:3)

在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(),您需要指定具体的标题列表。

    如果包含头文件取决于其他某些宏,则需要先检查此宏,或者再调用CHECK_SYMBOL_EXISTS()(无论宏是已定义),还是使用plain命令try_compile(检查宏的)。或者,您可以检查CheckIncludeFile模块是否存在特定标头。

  • 使用普通CHECK_SYMBOL_EXISTS()if(<var>)拨打电话后,可以发出取决于if(NOT <var>)结果的其他操作。