【问题标题】:How to check for compiler support of certain keywords/attributes?如何检查某些关键字/属性的编译器支持?
【发布时间】:2018-05-03 14:39:19
【问题描述】:

我想使用 CMake 来检查我的 C 编译器是否支持:

  • __hidden,或其等价物,例如 __attribute__ ((visibility ("hidden")))
  • restrict,或其等价物

这可能吗?如果有,怎么做?

【问题讨论】:

  • autoconf 用于测试某些功能和特性的支持等特性的方法是生成小型测试程序并运行编译器。

标签: c cmake compilation


【解决方案1】:

您可以通过尝试使用check_c_source_compiles 编译它们并将结果存储在make 变量中来检查各种功能。例如,您将在 CMakeLists.txt 中使用这些测试来检查 restrict__hidden 的可用性:

check_c_source_compiles(
    "
        int f(void *restrict x);
        int main(void) {return 0;}
    "
    HAVE_RESTRICT
)

check_c_source_compiles(
    "
        typedef struct s *t;
        int f(t __restrict x);
        int main(void) {return 0;}
    "
    HAVE___RESTRICT
)

check_c_source_compiles(
    "
        __hidden int f() {return 1;}
        int main(void) {return 0;}
    "
    HAVE___HIDDEN
)

check_c_source_compiles(
    "
        #include <stdlib.h>
        static void f(void) __attribute__ ((visibility(\"hidden\")));
        int main(void) {return 0;}
    "
    HAVE___ATTRIBUTE__VISIBILITY_HIDDEN
)

这里有一些例子:https://github.com/Kitware/CMake/blob/master/Utilities/cmliblzma/CMakeLists.txt

对于restrict 关键字,可用性可以通过c_restrictCMAKE_C_COMPILE_FEATURES 变量中的存在来确定:

if (c_restrict IN_LIST CMAKE_C_COMPILE_FEATURES)
    [...]
endif()

【讨论】:

  • 您能具体写下如何​​检查c_restrict 是否在编译器的功能集中吗?
  • 另外,这些是 0/1 值,还是被定义/未定义
猜你喜欢
  • 2014-02-28
  • 2015-06-10
  • 2011-07-13
  • 2013-11-06
  • 2013-02-01
  • 2019-12-20
  • 2016-05-01
  • 1970-01-01
  • 2011-06-26
相关资源
最近更新 更多