Linux gcc 不会编译 SDL C 程序(对 SDL 函数的未定义引用)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20625232/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:39:50  来源:igfitidea点击:

gcc won't compile SDL C Program (undefined reference to SDL functions)

clinuxgccsdl

提问by rullof

I recently moved to linux and i'm having an issue with compiling SDL C programs using gcc.

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

The command i'm using:

我正在使用的命令:

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

Even by seperating sdl-config flags:

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

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

I'm getting the same error:

我收到同样的错误:

/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

My very simple program:

我非常简单的程序:

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

采纳答案by Basile Starynkevitch

Order of arguments to gccmatters a lot. Replace

参数的顺序gcc很重要。代替

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

with

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

Better yet, learn how to use GNU makeand use a Makefileinspired by this answer...

更好的是,学习如何使用GNU make并使用Makefile受此答案启发的...

Also, always pass -Wall -gto gccuntil your program is bug-free (then use -Wall -O2)

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

回答by sujin

Add -lSDLwith gcc compile command. This will add sdl library. Install sdl developement package before compiling.

-lSDL使用 gcc 编译命令添加。这将添加 sdl 库。编译前安装sdl开发包。

EDIT:

编辑:

gcc -o out main.c -lSDL

or

或者

gcc -I/usr/include/SDL/ main.c -o out -L/usr/lib -lSDL

回答by Alvaro Denis Acosta

I See this from /usr/include/SDL2/SDL_main.h

我从 /usr/include/SDL2/SDL_main.h 看到这个

/* * This is called by the real SDL main function to let the rest of the * library know that initialization was done properly. * * Calling this yourself without knowing what you're doing can cause * crashes and hard to diagnose problems with your application. */ extern DECLSPEC void SDL_SetMainReady(void);

/* * 这由真正的 SDL 主函数调用,让 * 库的其余部分知道初始化已正确完成。* * 在不知道你在做什么的情况下自己调用它会导致 * 崩溃和难以诊断应用程序的问题。*/ extern DECLSPEC void SDL_SetMainReady(void);

Also check this: nm /usr/lib/i386-linux-gnu/libSDL.a | grep SDL_SetMainReady

还要检查这个: nm /usr/lib/i386-linux-gnu/libSDL.a | grep SDL_SetMainReady

This is not the solution but will allow you to focus on the real problem, I think it is not the compilation process.

这不是解决方案,但可以让您专注于真正的问题,我认为这不是编译过程。

回答by ericb

Thanks a lot for the advices ! This helped me to solve an old mystery about SDL symbols never found under Linux :-) As wroten in the comments, the order of gcc line is essential. The makefile was not well written and this was causing the breakage you mentionned.

非常感谢您的建议!这帮助我解决了一个关于在 Linux 下从未发现的 SDL 符号的古老谜团 :-) 正如评论中所写,gcc 行的顺序是必不可少的。生成文件写得不好,这导致了您提到的损坏。

As summary, use : gcc ( flags ) -o name ( include et linking options)

作为总结,使用: gcc ( flags ) -o name ( 包括 et 链接选项)

Last but not least, under x86_64, use gdb64, instead of gdb ( was the next headhache ;-)

最后但并非最不重要的是,在 x86_64 下,使用 gdb64,而不是 gdb(是下一个头痛;-)

As example, a little Makefile I wrote myself (ok, not that good, but works)

例如,我自己写的一个小 Makefile(好吧,不是很好,但有效)

# Makefile pour le contr?le du robot Arduino 
# Controle Robot
# Sauf mention contraire, tout est place sous Licence GPL V2
# Historique : 
# Etienne HAMON / création du makefile initial (d'après cours IFT1), Novembre 2014
#
# Corrections du Makefile : Eric Bachard décembre 2014 

CC = gcc
C_STANDARD = -std=c99
INCLUDE_DIR = inc -I/usr/include/SDL
SOURCES_DIR = sources
BUILD_DIR = build
APPLICATION_NAME = Controle
FILENAME = ${BUILD_DIR}/${APPLICATION_NAME}
CFLAGS = -Wall -ansi ${C_STANDARD}
LDFLAGS = -lSDL $(sdl-config --static-libs) -lm
DEBUG_SUFFIX = _debug
CFLAGS_DEBUG = -v -gdwarf-2 -DDEBUG
OBJS = ${SOURCES_DIR}/*.c

all : ${FILENAME} ${FILENAME}${DEBUG_SUFFIX}

${FILENAME}: ${OBJS}
    ${CC} ${CFLAGS} -o $@  $^ -I${INCLUDE_DIR} ${LDFLAGS} 

${FILENAME}${DEBUG_SUFFIX}: ${OBJS}
    ${CC} ${CFLAGS} ${CFLAGS_DEBUG} -o $@ $^ -I${INCLUDE_DIR} ${LDFLAGS} 

clean:
    ${RM} *.o ${FILENAME} ${FILENAME}${DEBUG_SUFFIX}
    ${RM} -rf ${BUILD_DIR}/*.dSYM

回答by awful_answers999

None of the "popular" answers for this question are correct or working. Some of them have nothing to even do with the question being asked. I have the same problem.

这个问题的“流行”答案都不是正确的或有效的。其中一些甚至与提出的问题无关。我也有同样的问题。

SDL docs give an example for compiling like this: gcc -o main main.c `sdl2-config --cflags --libs`

SDL 文档给出了这样编译的示例: gcc -o main main.c `sdl2-config --cflags --libs`

Yet user is suggesting that it is order of arguments causing the problem!! It is not! In fact, their suggested order is something I have never seen before and is certainly not any kind of standard. SDL answers on this site are very low quality!

然而,用户建议这是导致问题的参数顺序!!它不是!事实上,他们建议的顺序是我以前从未见过的,当然不是任何一种标准。此站点上的 SDL 答案质量非常低!

Update: I found a solution for some missing functions. SDL_SetVideoMode does not exist in SDL2. Use SDL_CreateWindow.

更新:我找到了一些缺失功能的解决方案。SDL2 中不存在 SDL_SetVideoMode。使用 SDL_CreateWindow。