libSDL,CMake和Mac OS X Lion

4

我正在尝试在我的Mac上编译一个cmake项目,但它依赖于SDL框架。我已经安装了这个框架,但在使用cmake时,它报告找不到libSDL。因此,我按照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;
}

1
你想要构建什么样的东西?是一个应用程序还是命令行工具或其他什么?CMake 抱怨缺少 main 函数。 - Michael Dautermann
这是make在抱怨,但本质上是一样的事情。我正在尝试编译一个SDL示例程序,所以我猜那应该是一个命令行工具。(记录一下,我确实有一个main函数,并且我没有看到任何对另一个main函数的调用。) - Mats
看一下那个示例代码,看看是否定义了任何“main”函数。如果没有,请创建一个(并确保它跳转到示例代码的真正入口点)。 - Michael Dautermann
好的,有一个主函数(我编辑了我的先前评论,也许你没有看到)。据我所见,它不调用其他主函数。 - Mats
1
为了让SDL能够正确处理跨平台开发中的主要定义差异(主要是winmain),SDL使用SDLMain.lib。我现在正在使用手机,无法访问我的项目以记住您需要在CMake中执行的操作。尝试打开findsdl cmake模块,并阅读文件顶部的注释。 - Ramon Zarazua B.
2个回答

1

我通过删除 .framework 文件和取消导出到该框架的变量来解决了它,然后我使用源代码进行了 ./configure、make 和 make install 操作。我删除了所有 cmake 构建文件,重新 cmake,然后重新构建我的项目,一切问题都得到了解决。

(在 OS X Lion 上编译 SDL 需要使用命令 ./configure --disable-assembly)


0
正如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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接