我正在使用 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
有任何想法吗?