【问题标题】:libSDL, CMake and Mac OS X LionlibSDL、CMake 和 Mac OS X Lion
【发布时间】:2011-12-29 12:39:39
【问题描述】:

我正在尝试在我的 mac 上编译一个 cmake 项目,但这取决于 SDL 框架。我安装了这个框架,然后在 cmake 之后向我报告 libSDL is not found 我自己设置了以下导出变量(如 cmake 所建议的那样):

export SDL_INCLUDE_DIR=/Library/Frameworks/SDL.framework/
export SDLIMAGE_LIBRARY=/Library/Frameworks/SDL_image.framework/
export SDLIMAGE_INCLUDE_DIR=/Library/Frameworks/SDL_image.framework/Headers

现在 cmake 可以正常工作,但是当我运行 make 时,我收到以下消息:

Mats-MBP:build mats$ make
Linking CXX executable SDLExample
Undefined symbols for architecture i386:
 "_main", referenced from:
     start in crt1.10.6.o
    (maybe you meant: _SDL_main)
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status
make[2]: *** [src/SDLExample] Error 1
make[1]: *** [src/CMakeFiles/SDLExample.dir/all] Error 2
make: *** [all] Error 2

我为 i386 和 x86_64 都得到了这个。我忘记了什么?

编辑:这些是文件内容:

Mats-MBP:build mats$ cat ../src/main.cpp 
/**
 * @file main.cpp
 *
 * A simple example-program to help you out with using SDL for 2D graphics.
 *
 * @author przemek
 */

#include <iostream>
#include <stdexcept>
#include <SDL.h>
#include <SDL_image.h>

using namespace std;

int main(int argc, char* argv[]) {
    try {
        // Try to initialize SDL (for video)
        if (SDL_Init(SDL_INIT_VIDEO) != 0) {
            throw runtime_error("Couldn't init SDL video!");
        }
        // Create a double-buffered screen surface:
        SDL_Surface* screen = SDL_SetVideoMode(800, 600, 24, SDL_DOUBLEBUF/* |     SDL_FULLSCREEN*/);
        if (!screen) {
            throw runtime_error("Couldn't set SDL video mode!");
        }

        // Use SDL_image library to load an image:
        SDL_Surface* image = IMG_Load("image.png");
//      SDL_Surface* image = IMG_Load("image.tga");
        if (!image) {
            throw runtime_error(SDL_GetError());
        }

        // "Pump" SDL events like keyboard presses and get the keystate:
        SDL_PumpEvents();
        Uint8* keyState = SDL_GetKeyState(0);

        // loop until user presses escape:
        while (!keyState[SDLK_ESCAPE]) {
            // Display a game background:
        SDL_Rect src, dst;          // Source and destination rectangles
        src.x = 0;
        src.y = 0;
        src.w = image->w;
        src.h = image->h;
        dst.x = 100;
        dst.y = 50;
        dst.w = image->w;
        dst.h = image->h;
        // Copy the image from 'image' to 'screen' surface
        SDL_BlitSurface(image, &src, SDL_GetVideoSurface(), &dst);

        // Flip surfaces (remember: double-buffering!) and clear the back buffer:
        SDL_Flip(screen);
        SDL_FillRect(screen, 0, 0);

        // Get new keyboard state:
        SDL_PumpEvents();
        keyState = SDL_GetKeyState(0);
    }

    // Free the screen surface & quit SDL
    SDL_FreeSurface(screen);
    SDL_Quit();
} catch (runtime_error& e) {
    cout << e.what() << endl;
    SDL_Quit();
}

return 0;
}

【问题讨论】:

  • 你想要构建什么样的东西?应用程序或命令行工具或其他东西? CMake 抱怨缺少 main 函数。
  • 这是在抱怨,但那是同一回事。我正在尝试编译一个 SDL 示例程序,所以我想这将是一个命令行工具。 (为了记录,我确实有一个主函数,我没有看到对另一个主函数的任何调用。)
  • 查看该示例代码,看看是否定义了任何“主”函数。如果没有,则创建一个(并确保它分支/跳转到示例代码的真正入口点)。
  • 嗯,有一个主要的(我编辑了我之前的评论,也许你没有看到)。据我所知,它没有调用其他主要功能。
  • 为了让 SDL 能够正确处理不同主定义(主要是 winmain)的跨平台开发,SDL 使用了 SDLMain.lib。我正在使用手机,因此无法访问我的项目来记住您需要在 CMake 中执行的操作。尝试打开 findsdl cmake 模块,并阅读文件顶部的注释

标签: c++ macos frameworks cmake sdl


【解决方案1】:

我通过删除 .framework 并取消设置我的导出变量到这个框架来解决它,我使用源代码来 ./configure、make、make install。我删除了所有 cmake 构建文件和 cmade 并再次制作了我的项目,一切都解决了。

(要在 OS X Lion 上编译 SDL,您需要 ./configure --disable-assembly

【讨论】:

    【解决方案2】:

    正如 Ramon 所说,对于许多平台,您需要链接到 SDLmain.lib。所以在你的 CMakeLists.txt 文件中有这样的东西。

    link_libraries (
       ${SDL_LIBRARY}
       ${SDLIMAGE_LIBRARY} # if using SDL_image, obviously
       SDLmain # Sadly not included in SDL_LIBRARY variable
    )
    

    这里是一个简单的页面了解更多信息:http://content.gpwiki.org/index.php/SDL:Tutorials:Setup

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-11
      • 2011-12-30
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2012-03-22
      • 2012-04-05
      相关资源
      最近更新 更多