2

私はDebianSqueezeを使用しており、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ターゲットの出力例-4つの関数すべてが未定義であることに注意してください

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

何か案は?

4

2 に答える 2

6

を使用できるはずです_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の単純なラッパーです。他のすべてが失敗した場合、あなたはそれを自分で書くことができるはずです。mallocfree

于 2012-06-02T12:21:34.173 に答える
4

GnulibのGNUページには、この関数がmingwプラットフォームにないことが記載されてpagealign_allocおり、これらのプラットフォームでGnulib関数を使用することを提案しています。

(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

align_allocC11には、配置を指定できるという名前の新しい関数があることに注意してください。

#include <stdlib.h>
void *aligned_alloc(size_t alignment, size_t size);
于 2012-06-02T12:07:56.840 に答える