【问题标题】:Undefined reference to posix_memalign using mingw32使用 mingw32 对 posix_memalign 的未定义引用
【发布时间】:2012-06-02 11:56:08
【问题描述】:

我正在使用 Debian Squeeze,使用 mingw32 交叉编译 windows 目标。

对于 Linux 目标,我可以使用 posix_memalign 来分配对齐的内存。

我似乎无法找到一种方法让它适用于 Windows 目标;我收到有关未定义引用的错误。我尝试了几种替代功能,均无济于事。

示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int main(void)
{
    char *foo;

    /* works on linux */
    posix_memalign(&foo, 1024, 1024);

    /* deprecated linux */
    memalign(1024, 1024);
    valloc(1024);

    /* should work on windows only */
    _aligned_malloc(1024, 1024);
}

Linux 目标的示例输出(预期):

ben@debian6400:~/folder$ gcc --version
gcc (Debian 4.4.5-8) 4.4.5

ben@debian6400:~/folder$ gcc -std=c99 test.c
test.c: In function ‘main’:
test.c:11: warning: implicit declaration of function ‘posix_memalign’
test.c:18: warning: implicit declaration of function ‘_aligned_malloc’
/tmp/ccPwPLsW.o: In function `main':
test.c:(.text+0x55): undefined reference to `_aligned_malloc'
collect2: ld returned 1 exit status

Windows 目标的示例输出 -- 请注意所有四个函数都未定义

ben@debian6400:~/folder$ i586-mingw32msvc-gcc --version
i586-mingw32msvc-gcc (GCC) 4.4.4

ben@debian6400:~/folder$ i586-mingw32msvc-gcc -std=c99 test.c
test.c: In function ‘main’:
test.c:14: warning: implicit declaration of function ‘posix_memalign’
test.c:17: warning: implicit declaration of function ‘memalign’
test.c:18: warning: implicit declaration of function ‘valloc’
test.c:21: warning: implicit declaration of function ‘_aligned_malloc’
/tmp/ccpH5Dsj.o:test.c:(.text+0x26): undefined reference to `_posix_memalign'
/tmp/ccpH5Dsj.o:test.c:(.text+0x3a): undefined reference to `_memalign'
/tmp/ccpH5Dsj.o:test.c:(.text+0x46): undefined reference to `_valloc'
/tmp/ccpH5Dsj.o:test.c:(.text+0x5a): undefined reference to `__aligned_malloc'
collect2: ld returned 1 exit status

有什么想法吗?

【问题讨论】:

    标签: c memory-management cross-compiling memory-alignment mingw32


    【解决方案1】:

    您应该可以使用_aligned_malloc。 mingw 中可能缺少其他功能。

    • 更新您的 mingw。它应该在 4.6.2 中工作。
    • 改用__mingw_aligned_malloc
    • 按此顺序包含您的标题:

      #include <stdlib.h>
      #include <intrin.h>
      #include <malloc.h>
      #include <windows.h>
      
    • _aligned_malloc/_aligned_free 只是malloc/free 的简单包装。如果其他一切都失败了,你应该可以自己写。

    【讨论】:

    • 链接“此订单”已损坏
    【解决方案2】:

    Gnulib 的 GNU 页面说 mingw 平台上缺少此功能,他们建议在这些平台上使用 Gnulib pagealign_alloc 功能。

    (8.632, posix_memalign) 某些平台缺少此功能:MacOS X 10.5、FreeBSD 6.0、NetBSD 3.0、OpenBSD 3.8、Minix 3.1.8、AIX 5.1、HP-UX 11、IRIX 6.5、OSF/1 5.1、 Solaris 10、Cygwin 1.5.x、mingw、MSVC 9、Interix 3.5、BeOS。

    Gnulib 模块 pagealign_alloc 提供了一个类似的 API,它返回在系统页面边界上对齐的内存。

    http://www.gnu.org/software/gnulib/manual/html_node/posix_005fmemalign.html

    请注意,在 C11 上,有一个名为 align_alloc 的新函数可以指定对齐方式:

    #include <stdlib.h>
    void *aligned_alloc(size_t alignment, size_t size);
    

    【讨论】:

      猜你喜欢
      • 2019-05-05
      • 2016-06-20
      • 2021-02-18
      • 1970-01-01
      • 2013-08-10
      • 2021-12-22
      • 1970-01-01
      • 2012-04-06
      • 2014-06-27
      相关资源
      最近更新 更多