c - gcc 不会编译 SDL C 程序(对 SDL 函数的 undefined reference )

标签 c linux gcc sdl

我最近转向了 linux,但在使用 gcc 编译 SDL C 程序时遇到了问题。

我正在使用的命令:

gcc `sdl-config --cflags --libs` -o main main.c

即使通过分离 sdl-config 标志:

gcc `sdl-config --cflags` -c main.c
gcc `sdl-config --libs` -o main main.o

我遇到了同样的错误:

/tmp/ccHYyjKd.o: In function `main':
main.c:(.text+0xe): undefined reference to `SDL_SetMainReady'
main.c:(.text+0x18): undefined reference to `SDL_Init'
main.c:(.text+0x31): undefined reference to `SDL_SetVideoMode'
main.c:(.text+0x54): undefined reference to `SDL_MapRGB'
main.c:(.text+0x6b): undefined reference to `SDL_FillRect'
main.c:(.text+0x77): undefined reference to `SDL_Flip'
main.c:(.text+0x83): undefined reference to `SDL_WaitEvent'
main.c:(.text+0x90): undefined reference to `SDL_Quit'
collect2: error: ld returned 1 exit status

我非常简单的程序:

#include <stdio.h>
#include <stdlib.h>
#define SDL_MAIN_HANDLED
#include <SDL/SDL.h>

int main()
{   
    // SDL Initialize
    SDL_SetMainReady();
    SDL_Init(SDL_INIT_VIDEO);

    // Screen Initialize
    SDL_Surface *screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    Uint32 screenColor = SDL_MapRGB(screen->format, 255, 255, 255);
    SDL_FillRect(screen, NULL, screenColor);
    SDL_Flip(screen);

    // Main Program Loop
    SDL_Event event;
    do
    {
        SDL_WaitEvent(&event);
    } while (event.type != SDL_QUIT);

    // SDL Quit
    SDL_Quit();

    return 0;
}

最佳答案

gcc 的参数顺序很重要。

了解 Invoking GCC (以及 gcc 使用的 binutils 的文档)。然后替换

 gcc `sdl-config --libs` -o main main.o

 gcc main.o  `sdl-config --libs` -o main

更好的是,学习如何使用 GNU make (它经常使用 GNU bash )并使用受 this answer 启发的 Makefile ...

此外,始终将 -Wall -g 传递给 gcc 直到您的程序没有错误(然后使用 -Wall -O2)

从开源程序中获取灵感 githubgitlab使用 SDL . 还可以考虑使用其他开源库和框架,例如 Qt , SFML , GTKmm等...并研究他们的示例代码。

关于c - gcc 不会编译 SDL C 程序(对 SDL 函数的 undefined reference ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20625232/

相关文章:

c - 我尝试进行的新系统调用有问题(Ubuntu 18.04.1 LTS 内核 :4. 17.4)

c - 如何从 execl 命令中捕获输出

c++11 - 如何使用旧的 ABI 通过 GCC 5 编译 boost?

c - "Undefined Symbol _memset"

c - 在 C 程序中重复使用 TCL 解释器

c - 尝试制作 irc 机器人

python - 如何从另一个 Python 脚本内部运行一个 Python 脚本?

linux - 如何在 postgres-8.4 触发器中调试 plperl 脚本

linux - 当下一条(跳过)指令是变量定义时,Shellcode 中的 JMP 意外行为

c++ - c++ 目标文件应该只通过 g++ 链接吗?