可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I recently moved to linux and i'm having an issue with compiling SDL C programs using gcc.
The command i'm using:
gcc `sdl-config --cflags --libs` -o main main.c
Even by seperating sdl-config flags:
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;
}
回答1:
Order of arguments to gcc
matters a lot. Replace
gcc `sdl-config --libs` -o main main.o
with
gcc main.o `sdl-config --libs` -o main
Better yet, learn how to use GNU make and use a Makefile
inspired by this answer...
Also, always pass -Wall -g
to gcc
until your program is bug-free (then use -Wall -O2
)
回答2:
Add -lSDL
with gcc compile command. This will add sdl library. Install sdl developement package before compiling.
EDIT:
gcc -o out main.c -lSDL
or
gcc -I/usr/include/SDL/ main.c -o out -L/usr/lib -lSDL
回答3:
I See this from /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);
Also check this:
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.
回答4:
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.
As summary, use : gcc ( flags ) -o name ( include et linking options)
Last but not least, under x86_64, use gdb64, instead of gdb ( was the next headhache ;-)
As example, a little Makefile I wrote myself (ok, not that good, but works)
# 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
回答5:
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`
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!
Update:
I found a solution for some missing functions. SDL_SetVideoMode does not exist in SDL2. Use SDL_CreateWindow.