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

前端 未结 5 1566
栀梦
栀梦 2021-01-14 18:32

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`          


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-14 19:04

    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
    

提交回复
热议问题