C 在OSX上运行Allegro时出错
我在OSX上运行了brew安装allegro 遵循本教程: 我的代码C 在OSX上运行Allegro时出错,c,macos,allegro,C,Macos,Allegro,我在OSX上运行了brew安装allegro 遵循本教程: 我的代码 include <allegro.h> int main(void) { if (allegro_init() != 0) return 1; /* set up the keyboard handler */ install_keyboard(); /* set a graphics mode sized 320x200 */ if (set_gfx_mode(GFX_AU
include <allegro.h>
int main(void) {
if (allegro_init() != 0)
return 1;
/* set up the keyboard handler */
install_keyboard();
/* set a graphics mode sized 320x200 */
if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
return 1;
}
}
/* set the color palette */
set_palette(desktop_palette);
/* clear the screen to white */
clear_to_color(screen, makecol(255, 255, 255));
/* you don't need to do this, but on some platforms (eg. Windows) things
* will be drawn more quickly if you always acquire the screen before
* trying to draw onto it.
*/
acquire_screen();
/* write some text to the screen with black letters and transparent background */
textout_centre_ex(screen, font, "Hello, world!", SCREEN_W/2, SCREEN_H/2, makecol(0,0,0), -1);
/* you must always release bitmaps before calling any input functions */
release_screen();
/* wait for a keypress */
readkey();
return 0;
}
1.c:1:1: error: unknown type name 'include'
include <allegro.h>
^
1.c:1:9: error: expected identifier or '('
include <allegro.h>
^
2 errors generated.
make: *** [1] Error 1
包括
int main(void){
如果(allegro_init()!=0)
返回1;
/*设置键盘处理程序*/
安装键盘();
/*设置大小为320x200的图形模式*/
如果(设置gfx模式(gfx自动检测,320,200,0,0)!=0){
如果(设置gfx模式(gfx安全,320200,0,0)!=0){
设置_gfx_模式(gfx_文本,0,0,0);
allegro_消息(“无法设置任何图形模式\n%s\n”,allegro_错误);
返回1;
}
}
/*设置调色板*/
设置调色板(桌面调色板);
/*将屏幕清除为白色*/
清除颜色(屏幕,makecol(255,255,255));
/*您不需要这样做,但在某些平台(如Windows)上需要这样做
*如果您总是在之前获取屏幕,将更快地绘制
*试图利用它。
*/
获取屏幕();
/*用黑色字母和透明背景在屏幕上写一些文字*/
textout_center_ex(屏幕,字体,“你好,世界!”,屏幕W/2,屏幕H/2,makecol(0,0,0),-1);
/*在调用任何输入函数之前,必须始终释放位图*/
释放屏幕();
/*等待按键*/
readkey();
返回0;
}
1.c:1:1:错误:未知类型名称“include”
包括
^
1.c:1:9:错误:应为标识符或'('
包括
^
产生2个错误。
make:**[1]错误1
假设doinclude的打字错误显示了一些差异:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) { // allegro_init in allegro4
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480); // very different to allegro4
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0)); // makecol -> al_map_rgb, clear_to_color -> al_clear_to_color
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
<代码>文件>代码> AdRoRyStudio.cc。注意我使用C++编译器编译,因为AlelGro实际上是一个C++ API(并且当编译成C代码时,该示例不起作用,因为C中没有结构调用约定,而C++是存在的)。我错误地认为印象中的快板是纯C库。谢谢,这画了一个黑色矩形窗口。我在C vs C++问题上的推理是,当我用C编译器编译这个例子时,用<代码> alsiMAPJRGB(255255255)代替<代码> AlsiMAPJRGB(0,0,0)<代码>。程序将与SeGV发生冲突,这是使用C++编译器避免的二进制兼容性问题或编译器问题的一种形式。
c++ -I/usr/local/include allegro_display.cc -o allegro_display -L/usr/local/lib -lallegro -lallegro_main