2

我正在使用Debian Squeeze,使用mingw32为Windows目标交叉编译。使用mingw32未定义对posix_memalign的引用

对于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目标(预计):

[email protected]:~/folder$ gcc --version 
gcc (Debian 4.4.5-8) 4.4.5 

[email protected]:~/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目标 - 注意,所有四个功能是不确定的

[email protected]:~/folder$ i586-mingw32msvc-gcc --version 
i586-mingw32msvc-gcc (GCC) 4.4.4 

[email protected]:~/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 

有没有想法?

回答

2

您应该能够使用_aligned_malloc。在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。如果一切都失败了,你应该可以自己写。

+0

__mingw_aligned_malloc工作!谢谢! – HoboBen

+1

链接“此订单”已损坏 –

3

为Gnulib GNU的网页说,这个功能缺失对MinGW的平台,他们建议使用Gnulib pagealign_alloc功能在这些平台上。

(8.632,posix_memalign)该函数缺少在某些平台上:的MacOS X 10.5,FreeBSD的6.0,NetBSD的3.0,3.8的OpenBSD,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);