在C Makefile中引用SDL的错误(没有IDE)

问题描述 投票:-2回答:1

我有以下错误:

/usr/bin/ld: pixel_operations.o: in function `update_surface':
pixel_operations.c:(.text+0xf7): undefined reference to `SDL_UpperBlit'
/usr/bin/ld: pixel_operations.c:(.text+0x121): undefined reference to `SDL_GetError'
/usr/bin/ld: pixel_operations.c:(.text+0x114): undefined reference to `SDL_UpdateRect'

我有以下Makefile:

CFLAGS= -Wall -Wextra -Werror -std=c99 -O3
CPPFLAGS=`pkg-config --cflags sdl` -MMD
LDFLAGS=
LDLIBS= `pkg-config --libs sdl` -lSDL_image
EXEC= main
SRC= matrix.o random.o pixel_operations.o layer.c train.c neural_net.c main.c save_net.c

all: net

net: ${SRC}
    gcc ${CPPFLAGS} ${CFLAGS} -o ${EXEC} ${SRC} -lm

matrix.o: ../Misc/matrix.c
    gcc ${CFLAGS} -o matrix.o -c ../Misc/matrix.c

random.o: ../Misc/random.c
    gcc ${CFLAGS} -o random.o -c ../Misc/random.c

otsu.o: ../Binarize/otsu.c
    gcc ${CFLAGS} -o otsu.o -c ../Binarize/otsu.c

pixel_operations.o: ../Binarize/pixel_operations.c
    gcc ${CFLAGS} ${CPPFLAGS} -o pixel_operations.o -c ../Binarize/pixel_operations.c ${LDLIBS}

clean:
    rm -f *.o
    rm -f ../Misc/*.d
    rm -f *.txt
    rm ${EXEC}

我不明白为什么在使用pkg-config时未引用SDL。 SDL用于pixel_operation.c和otsu.c。我所做的一些研究表明,库必须位于编译命令的末尾,但就我而言,无论我放在哪里,它都行不通。我该如何解决?

c makefile sdl
1个回答
0
投票

您将${LDLIBS}放置在错误的位置。

应该类似于以下内容:

CFLAGS= -Wall -Wextra -Werror -std=c99 -O3
CPPFLAGS=`pkg-config --cflags sdl` -MMD
LDFLAGS=
LDLIBS= `pkg-config --libs sdl` -lSDL_image
SRC= matrix.o random.o pixel_operations.o layer.o train.o neural_net.o main.o save_net.o

all: net

# also note that the dependencies of the final executable are object files, not sources
net: ${SRC} # LDLIBS here instead:
    gcc ${CPPFLAGS} ${CFLAGS} -o ${EXEC} ${SRC} -lm ${LDLIBS}

matrix.o: ../Misc/matrix.c
    gcc ${CFLAGS} -o matrix.o -c ../Misc/matrix.c

random.o: ../Misc/random.c
    gcc ${CFLAGS} -o random.o -c ../Misc/random.c

otsu.o: ../Binarize/otsu.c
    gcc ${CFLAGS} -o otsu.o -c ../Binarize/otsu.c

pixel_operations.o: ../Binarize/pixel_operations.c # no LDLIBS here:
    gcc ${CFLAGS} ${CPPFLAGS} -o pixel_operations.o -c ../Binarize/pixel_operations.c 

clean:
    rm -f *.o
    rm -f ../Misc/*.d
    rm -f *.txt
    rm ${EXEC}
© www.soinside.com 2019 - 2024. All rights reserved.