SDL 链接器错误

发布于 2024-12-01 18:44:19 字数 2840 浏览 0 评论 0原文

下面的代码非常简单。它只是使用 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

The following code is pretty simple. It just draws an image to the screen with a background using SDL. The program I was able to run before hand did fine, referencing everything without a problem.

Now, however, the compiler complains.

Anyway, here's the code:

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

}

And my output:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2024-12-08 18:44:19

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

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

You're not linking in the SDL library. Try this:

g++ main.o `sdl-config --libs`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文