al_draw_filled_rectangle 中的分段错误

问题描述 投票:0回答:1

我是在 Windows 上使用 mingw c++ 的新手。我最近创建了一个简单的程序,只是在蓝色背景上绘制一个黑色方块,以确保一切正常运行并正确安装,但是当我通过 gdb 运行该程序时,它返回一个错误。

这是我的代码:

#include <allegro5/allegro5.h>
#include <allegro5/allegro_primitives.h>

#include <iostream>

int main(int argc, char* argv[])
{
    al_init();
    
    ALLEGRO_DISPLAY* d = al_create_display(256, 256);
    
    if (d == NULL)
        std::cout << "Error in initializing display" << std::endl;
    
    al_clear_to_color(al_map_rgb(0, 0, 255));
    al_draw_filled_rectangle(64, 64, 192, 192, al_map_rgb(0, 0, 0));
    al_flip_display();
    
    al_rest(30);
    
    al_destroy_display(d);
    al_uninstall_system();
    return 0;
}

我用这个命令编译它:

g++ -I "C:\msys64\mingw64\include\" test.cpp -o test.exe -g -lallegro
 -lallegro_primitives

它编译得很好,但是当我尝试使用

gdb .\test.exe
运行它时,它返回此错误:

Thread 1 received signal SIGSEGV, Segmentation fault.
al_lock_mutex (mutex=0x0) at C:/dev/allegro_winpkg/universal/allegro/src/threads.c:325
warning: 325    C:/dev/allegro_winpkg/universal/allegro/src/threads.c: No such file or directory

这是完整的堆栈跟踪:

#0  al_lock_mutex (mutex=0x0) at C:/dev/allegro_winpkg/universal/allegro/src/threads.c:325
#1  0x00007ffc351282bc in get_display_local_data (display=display@entry=0x616fa0)
    at C:/dev/allegro_winpkg/universal/allegro/addons/primitives/prim_directx.cpp:138
#2  0x00007ffc35129a4b in draw_prim_raw (target=0x617280, texture=0x0, vtx=0x5ffd90, decl=0x0, indices=0x0, num_vtx=4,
    type=5) at C:/dev/allegro_winpkg/universal/allegro/addons/primitives/prim_directx.cpp:500
#3  0x00007ffc3512a011 in _al_draw_prim_directx (target=target@entry=0x617280, texture=texture@entry=0x0,
    vtxs=vtxs@entry=0x5ffd90, decl=decl@entry=0x0, start=<optimized out>, start@entry=0, end=<optimized out>,
    end@entry=4, type=<optimized out>, type@entry=5)
    at C:/dev/allegro_winpkg/universal/allegro/addons/primitives/prim_directx.cpp:656
#4  0x00007ffc3512e048 in al_draw_prim (vtxs=0x5ffd90, decl=0x0, texture=0x0, start=0, end=4, type=5)
    at C:/dev/allegro_winpkg/universal/allegro/addons/primitives/primitives.c:104
#5  0x00007ffc35122784 in al_draw_filled_rectangle (x1=<optimized out>, y1=0, x2=<optimized out>, y2=0, color=...)
    at C:/dev/allegro_winpkg/universal/allegro/addons/primitives/high_primitives.c:422
#6  0x00007ff7a78f1553 in main (argc=1, argv=0xf61b60) at test.cpp:16

我猜我在 mingw 的设置过程中错过了某些步骤,但我不知道如何修复它。有谁有答案吗

c++ mingw mingw-w64 allegro allegro5
1个回答
0
投票

在我的Linux系统上,你的程序运行良好,但对于Windows你可能需要调用(如评论中所述):

al_init_primitives_addon();

在您可以使用诸如

al_draw_filled_rectangle
之类的功能之前。

© www.soinside.com 2019 - 2024. All rights reserved.