问题
I'm using Debian Squeeze, cross compiling for windows targets using mingw32.
For a Linux target, I can use posix_memalign to allocate aligned memory.
I can't seem to find a way to get this to work for windows targets; I get errors about undefined references. I have tried several alternative functions, to no avail.
Example Code:
#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);
}
Example output for a Linux target (expected):
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
Example output for a Windows target -- note that all four functions are undefined
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
Any ideas?
回答1:
You should be able to use _aligned_malloc
. Other functions can be missing in mingw.
- Update your mingw. It should work in 4.6.2.
- Try
__mingw_aligned_malloc
instead. Include your headers in this order:
#include <stdlib.h> #include <intrin.h> #include <malloc.h> #include <windows.h>
_aligned_malloc
/_aligned_free
is just a simple wrapper aroundmalloc
/free
. If everything else fails, you should be able to write it yourself.
回答2:
The GNU page for Gnulib says this function is missing on mingw platform and they suggest to use Gnulib pagealign_alloc
function on these platforms.
(8.632, posix_memalign) This function is missing on some platforms: 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.
and
The Gnulib module pagealign_alloc provides a similar API that returns memory aligned on a system page boundary.
http://www.gnu.org/software/gnulib/manual/html_node/posix_005fmemalign.html
Note that on C11, there is a new function named align_alloc
where the alignment can be specified:
#include <stdlib.h>
void *aligned_alloc(size_t alignment, size_t size);
来源:https://stackoverflow.com/questions/10862121/undefined-reference-to-posix-memalign-using-mingw32