首页 > 解决方案 > 英特尔编译器使用“属性“__malloc__”不接受参数的 C++14 检查失败

问题描述

我有以下代码(旨在检测编译器是否支持 C++14):

#include <memory>
#include <algorithm>

// Check the version language macro, but skip MSVC because
// MSVC reports 199711 even in MSVC 2017.
#if __cplusplus < 201402L && !defined(_MSC_VER) && !defined(__INTEL_COMPILER)
#error "insufficient support for C++14"
#endif

int main()
{
    auto ptr = std::make_unique<int>(42);
    constexpr int max = std::max(0, 1);
    (void) ptr;
    (void) max;
    return 0;
}

使用g++(版本 11.2.1)编译它时,g++ -std=c++14 test.cpp -o test它工作正常。当使用 intel 编译器(版本 2021.3.0(gcc 版本 11.2.1 兼容性))而不是使用 编译它时icpc -std=c++14 test.cpp -o test,它会失败并显示

In file included from /usr/include/c++/11/cwchar(44),
                 from /usr/include/c++/11/bits/postypes.h(40),
                 from /usr/include/c++/11/iosfwd(40),
                 from /usr/include/c++/11/bits/shared_ptr.h(52),
                 from /usr/include/c++/11/memory(77),
                 from test.cpp(1):
/usr/include/wchar.h(155): error: attribute "__malloc__" does not take arguments
    __attribute_malloc__ __attr_dealloc_free;
                         ^

In file included from /usr/include/c++/11/cstdlib(75),
                 from /usr/include/c++/11/bits/stl_algo.h(59),
                 from /usr/include/c++/11/algorithm(62),
                 from test.cpp(2):
/usr/include/stdlib.h(565): error: attribute "__malloc__" does not take arguments
      __attr_dealloc_free;
      ^

In file included from /usr/include/c++/11/cstdlib(75),
                 from /usr/include/c++/11/bits/stl_algo.h(59),
                 from /usr/include/c++/11/algorithm(62),
                 from test.cpp(2):
/usr/include/stdlib.h(569): error: attribute "__malloc__" does not take arguments
       __THROW __attr_dealloc (reallocarray, 1);
               ^

In file included from /usr/include/c++/11/cstdlib(75),
                 from /usr/include/c++/11/bits/stl_algo.h(59),
                 from /usr/include/c++/11/algorithm(62),
                 from test.cpp(2):
/usr/include/stdlib.h(797): error: attribute "__malloc__" does not take arguments
       __attr_dealloc_free __wur;
       ^

compilation aborted for test.cpp (code 2)

这里到底出了什么问题,我该如何解决?

简短更新:看起来 CUDA 遇到了类似的问题,并且可能与 glibc 2.34 有关:https ://forums.developer.nvidia.com/t/cuda-11-5-samples-throw-multiple-error-属性-malloc-does-not-take-arguments/192750/15

标签: c++c++14intelicc

解决方案


使用 icpc 2021.4 编译和执行共享代码,运行良好。

使用下面的命令来编译代码。icpc -std=c++14

下面是环境细节。

操作系统:Ubuntu 18.04.3 LTS 内核:Linux 4.15.0-76-generic

有关兼容性,请参阅英特尔® C++ 编译器经典系统要求的链接

https://software.intel.com/content/www/us/en/develop/articles/oneapi-c-compiler-system-requirements.html


推荐阅读