c - 在 OSX 上运行 Allegro 时出错

标签 c macos allegro

我在 OSX 上运行了brew install allegro

按照本教程操作:https://wiki.allegro.cc/index.php?title=Example_ExHello

我的代码

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

最佳答案

假设做 include <allegro.h> 的拼写错误应该是#include <allegro.h> ,您已经安装了 allegro5 - allegro4(本示例来自)和 allegro5 之间的 API 非常不同。显示初始化sample program for allegro5显示了一些差异:

#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;
}

我使用以下方法构建它:

 c++ -I/usr/local/include allegro_display.cc -o allegro_display -L/usr/local/lib -lallegro -lallegro_main

代码在文件 allegro_display.cc 中的位置。请注意,我使用 C++ 编译器进行编译,因为 allegro 实际上是一个 C++ api(并且该示例在编译为 C 代码时不起作用,因为 C 中的结构没有正确的调用约定,而 C++ 中有)

关于c - 在 OSX 上运行 Allegro 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42719977/

相关文章:

c - PIC单片机的FTP客户端

c - 使用 connect() 函数时收到 errno EMLINK

c - 带开罗的 Gouraud 阴影三角形

ios - CloudKit 记录版本控制

c++ - Mac OSX 构建中缺少 Allegro 5.2 TTF 插件

audio - Allegro 声音不起作用(播放 wav 文件)

c - Linux内核模块编程

macos - Mac OS X 中您最喜欢的反汇编工具是什么?

c++ - 加载位图时 Allegro 崩溃

c++ - 错误 : out-of-line definition of 'test' does not match any declaration in 'B<dim>'