c++ - SDL 链接器错误

标签 c++ sdl runtime-error

下面的代码非常简单。它只是使用 SDL 将图像绘制到带有背景的屏幕上。我之前能够运行的程序运行良好,可以毫无问题地引用所有内容。

然而,现在编译器会报错。

无论如何,这是代码:

#include <iostream>
#include <string>
#include "SDL/SDL.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

using std::string;

SDL_Surface * load_image(std::string filename)
{
    SDL_Surface * loadedImage = NULL;

    SDL_Surface * optimizedImage = NULL;

    loadedImage = SDL_LoadBMP(filename.c_str());

    if (loadedImage != NULL)
    {
        optimizedImage = SDL_DisplayFormat(loadedImage);

        SDL_FreeSurface(loadedImage);
    }

    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface * source, SDL_Surface * destination)
{
    SDL_Rect offset;

    //Give offsets to the rectangle
    offset.x = x;
    offset.y = y;

    SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char * args[])
{

    if ( SDL_Init(SDL_INIT_EVERYTHING) == -1 ) 
    {
        return 1;
    }
    else
    {   

      SDL_Surface * screen = SDL_SetVideoMode(SCREEN_WIDTH, 
                                   SCREEN_HEIGHT, 
                                   SCREEN_BPP,                                     
                                   SDL_SWSURFACE);

        if (screen == NULL)
        {
            return 1;

        }
        else
        {
            SDL_WM_SetCaption("Hello World", NULL);

            SDL_Surface * message = load_image("hello.bmp");
            SDL_Surface * background = load_image("background.bmp");

            apply_surface(0, 0, background, screen);
            apply_surface(320, 0, background, screen);
            apply_surface(0, 240, background, screen);
            apply_surface(320, 240, background, screen);

            if (SDL_Flip( screen ) == -1)
            {
                return 1;
            }
        }


    }

    return 0;

}

我的输出:

holland@holland-litevision:~/code/cpp$ g++ main.o
main.o: In function `load_image(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
main.cpp:(.text+0x2b): undefined reference to `SDL_RWFromFile'
main.cpp:(.text+0x3b): undefined reference to `SDL_LoadBMP_RW'
main.cpp:(.text+0x4f): undefined reference to `SDL_DisplayFormat'
main.cpp:(.text+0x5d): undefined reference to `SDL_FreeSurface'
main.o: In function `apply_surface(int, int, SDL_Surface*, SDL_Surface*)':
main.cpp:(.text+0x97): undefined reference to `SDL_UpperBlit'
main.o: In function `main':
main.cpp:(.text+0xaf): undefined reference to `SDL_Init'
main.cpp:(.text+0xe7): undefined reference to `SDL_SetVideoMode'
main.cpp:(.text+0x110): undefined reference to `SDL_WM_SetCaption'
main.cpp:(.text+0x24c): undefined reference to `SDL_Flip'
collect2: ld returned 1 exit status

最佳答案

您没有在 SDL 库中进行链接。试试这个:

g++ main.o `sdl-config --libs`

关于c++ - SDL 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7219018/

相关文章:

c++ - 无法重载现有的 std::vector 函数

c++ - 为什么1个元素的数组允许2k个元素?

c++ - glCreateShader失败并返回0

c - 运行我的程序时出现运行时错误

bash - Fortran execute_command_line 运行时错误,取决于内存消耗

java - 在 Storm 官方 docker 1.2.1 上运行拓扑时出现错误 - "Could not find or load main class "

c++ - 在 C 中查找 100 位数字 mod 10^9+7 的值

c++ - 使用参数包将 lambda 转换为 std::function

c++ - 陷入构建游戏引擎的困境

c++ - 需要一个客户端交互式 2D 世界地图 : best map package? 或者最好的 C++ 图形/ Canvas 库来制作一个?