编译器无法找到与QUOT; aligned_alloc"功能 [英] compiler can't find "aligned_alloc" function

查看:1174
本文介绍了编译器无法找到与QUOT; aligned_alloc"功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让一个便携式的函数,在堆上分配对齐功能。

找到aligned_alloc,我认为这是stdlib.h中,但海湾合作委员会似乎不这么认为。


  

错误:aligned_alloc在此范围未声明


使用gcc旗子

-std = C ++ 11 -m64

试图导入

 的#include<&stdlib.h中GT;
#包括LT&; cstdlib>


解决方案

aligned_alloc 在C11定义。据(还)没有定义用于C ++,尽管它可能在C ++中17中显示。 (也就是说,它是不是在列表中(目前)209的功能在标准C库,预计将在标准C ++库可参见C ++标准的附录C)。

如果你想用gcc使用它(或者更确切地说,与G ++),你应该使用功能测试宏 _ISOC11_SOURCE 。像所有的功能测试宏,宏必须在每一个需要它之前的任何文件的#define 'D 的#include 。 [见注1](功能测试宏不会,如果你用 -std = C11 编译一个C程序是必要的。)

我发现从男人aligned_alloc 正确的功能测试宏:


  

特性测试的glibc的宏要求(见feature_test_macros(7)):

  posix_memalign():_POSIX_C_SOURCE> = 200112L || _XOPEN_SOURCE> = 600  aligned_alloc():_ISOC11_SOURCE


作为指示,你可以阅读

 人7 feature_test_macros

有关功能测试宏的详细信息,包括glibc的认可宏的列表,并在每一个应用。

aligned_alloc 手册页也记录了非常相似的 posix_memalign 功能,这一直以来发行了POSIX标准的一部分6,并且具有不同的(更常见的)功能测试宏,如上所述。 posix_memalign 应该可以移植到任何Posix的系统,这可能会或可能不会包括比那些接受C ++使用C11功能的多个系统。

注释


  1. 这意味着你不应该把的#define 在头文件,因为头文件不能被包含之前,首先包括:)但是,如果你使用的东西像的#includeconfig.h中来包括平台的定义,每一个源文件与开始的#includeconfig.h中,任何其他之前的#include 的#define 发生在配置。 ^ h 之前的任何的#include ,你应该确定。

Trying to get a portable function to allocate on heap with aligned feature.

Found "aligned_alloc", which I think is in stdlib.h but gcc seems to not think so

error: 'aligned_alloc' was not declared in this scope

using gcc with flags -std=c++11 -m64

tried importing

#include <stdlib.h>
#include <cstdlib>

解决方案

aligned_alloc is defined in C11. It is not (yet) defined for C++, although it may show up in C++17. (That is, it is not in the list of (currently) 209 functions in the standard C library which are expected to be available in the standard C++ library. See Appendix C of the C++ standard.)

If you want to use it with gcc (or, more specifically, with g++), you should use the feature test macro _ISOC11_SOURCE. Like all feature test macros, this macro must be #define'd in every file which requires it before any #include. [See note 1] (The feature test macro would not be necessary if you were compiling a C program with -std=c11.)

I found the correct feature test macro from man aligned_alloc:

Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

  posix_memalign(): _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600

  aligned_alloc(): _ISOC11_SOURCE

As that indicates, you can read

man 7 feature_test_macros

for more information on feature test macros, including a list of the macros recognized by glibc, and when each one applies.

The aligned_alloc manpage also documents the very similar posix_memalign function, which has been part of the Posix standard since Issue 6, and which has different (and more common) feature test macros, as indicated above. posix_memalign should be portable to any Posix system, which might or might not include more systems than those that accept the use of a C11 function in C++.

Notes

  1. This means you shouldn't put the #define in a header file, since the header file cannot be included before the first include :) However, if you use something like #include "config.h" to include platform definitions, and every source file starts with #include "config.h", prior to any other #include, and the #define occurs in config.h prior to any #include, you should be OK.

这篇关于编译器无法找到与QUOT; aligned_alloc&QUOT;功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆