【问题标题】:Main function not found when running Allegro 5 on OSX在 OSX 上运行 Allegro 5 时找不到主要功能
【发布时间】:2016-06-04 16:58:12
【问题描述】:

我正在尝试获取在 OSX 上运行的 Allegro 的最小示例。

我使用 Homebrew 安装了稳定版 Allegro 5.2,as specified in the Allegro wiki

这是我的代码 (allegro.hpp):

/*
 * This program uses the Allegro game library to display a blank window.
 *
 * It initializes the display and starts up the main game loop. The
 * game loop only checks for two events: timer (determined by the FPS)
 * and display close (when the user tries to close the window).
 *
 * http://www.damienradtke.org/building-a-mario-clone-with-allegro
 */

#include <stdio.h>
#include <allegro5/allegro.h>

constexpr float FPS = 60.0f;

int main(int argc, char** argv) {
  ALLEGRO_DISPLAY *display = NULL;
  ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  ALLEGRO_TIMER *timer = NULL;

  bool running = true;
  bool redraw = true;

  // Initialize allegro
  if (!al_init()) {
    fprintf(stderr, "Failed to initialize allegro.\n");
    return 1;
  }

  // Initialize the timer
  timer = al_create_timer(1.0f / FPS);
  if (!timer) {
    fprintf(stderr, "Failed to create timer.\n");
    return 1;
  }

  // Create the display
  display = al_create_display(640, 480);
  if (!display) {
    fprintf(stderr, "Failed to create display.\n");
    return 1;
  }

  // Create the event queue
  event_queue = al_create_event_queue();
  if (!event_queue) {
    fprintf(stderr, "Failed to create event queue.");
    return 1;
  }

  // Register event sources
  al_register_event_source(event_queue, al_get_display_event_source(display));
  al_register_event_source(event_queue, al_get_timer_event_source(timer));

  // Display a blank screen
  al_clear_to_color(al_map_rgb(100, 149, 237));
  al_flip_display();

  // Start the timer
  al_start_timer(timer);

  // Game loop
  while (running) {
    ALLEGRO_EVENT event;
    ALLEGRO_TIMEOUT timeout;

    // Initialize timeout
    al_init_timeout(&timeout, 0.06);

    // Fetch the event (if one exists)
    bool get_event = al_wait_for_event_until(event_queue, &event, &timeout);

    // Handle the event
    if (get_event) {
      switch (event.type) {
        case ALLEGRO_EVENT_TIMER:
          redraw = true;
          break;
        case ALLEGRO_EVENT_DISPLAY_CLOSE:
          running = false;
          break;
        default:
          fprintf(stderr, "Unsupported event received: %d\n", event.type);
          break;
      }
    }

    // Check if we need to redraw
    if (redraw && al_is_event_queue_empty(event_queue)) {
      // Redraw
      al_clear_to_color(al_map_rgb(100, 149, 237));
      al_flip_display();
      redraw = false;
    }
  } 

  // Clean up
  al_destroy_display(display);
  al_destroy_event_queue(event_queue);

  return 0;
}

这是我的构建和执行命令:

$ clang++ -I/usr/local/include/ -L/usr/local/lib -lallegro -lallegro_main allegro.hpp -std=c++14 && ./a.out

这是我收到的错误:

dyld: Symbol not found: __al_mangled_main
  Referenced from: /usr/local/opt/allegro/lib/liballegro_main.5.2.dylib
  Expected in: flat namespace
 in /usr/local/opt/allegro/lib/liballegro_main.5.2.dylib
Trace/BPT trap: 5

我已经尝试过其他地方给出的解决方案:

  1. 将主函数的签名更改为int main(int argc, char** argv)
  2. 链接liballegro_main.dylib

【问题讨论】:

    标签: c++ macos allegro allegro5


    【解决方案1】:

    您将其命名为allegro.hpp,尽管它是源文件,而不是头文件

    将其重命名为 <i>something</i>.cpp<i>something</i>.cc

    【讨论】:

    • 谢谢,成功了!为什么会有所作为?不同文件扩展名的编译过程是否会发生变化?
    • 是和不是。不同文件类型的编译更改。文件类型通常由文件扩展名决定。我想你可以告诉clang 什么是头文件,什么是源文件,但我从来没有这样做过,你也不应该这样做。这就是扩展的用途。当你编译一个头文件时,你所做的是创建一个precompiled header,而不是一个可执行文件。如果您不添加-o 开关,您通常应该会看到allegro.hpp.gchallegro.pch 之类的文件。
    【解决方案2】:

    假设您按照建议将文件重命名为 allegro.cpp,那么这将是构建过程,使用 make

    obj =  allegro
    CPPFLAGS += -I/usr/local/include
    LDFLAGS += -L/usr/local/lib
    LDLIBS += -lallegro
    LDLIBS += -lallegro_main
    
    .PHONY: all
    all : $(obj)
    
    allegro: allegro.cpp
    

    【讨论】:

      【解决方案3】:

      如果你想使用 cmake ...

      这应该会让你继续前进

      cmake_minimum_required(VERSION 3.17)
      project(hi)
      
      set(CMAKE_CXX_STANDARD 14)
      # These are where the include files should be
      include_directories(/usr/local/include)
      
      # add extra lib directories
      link_directories(/usr/local/lib)
      
      add_executable(hi audio2.cpp)
      
      # specify the dependency on an extra library
      target_link_libraries(hi allegro allegro_main allegro_acodec allegro_audio)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-13
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        • 1970-01-01
        • 2015-03-17
        • 1970-01-01
        相关资源
        最近更新 更多