Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

c - 在C Makefile中引用SDL时出错(无IDE)(Error in referencing SDL in C Makefile (no IDE))

I have the following error:

(我有以下错误:)

/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'

I have the following Makefile:

(我有以下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}

I don't understand why SDL is not referenced as I used pkg-config.

(我不明白为什么在使用pkg-config时未引用SDL。)

SDL is used in pixel_operation.c and otsu.c.

(SDL用于pixel_operation.c和otsu.c。)

Some of the research I made said that the libs must be at the end of the compile command but in my case, regardless of where I put it it doesn't work.

(我做过的一些研究说,库必须在编译命令的末尾,但是就我而言,无论我放在哪里,它都行不通。)

How can I fix this ?

(我怎样才能解决这个问题 ?)

  ask by Firox translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You placed your ${LDLIBS} in the wrong place.

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

It should be rather like the following:

(它应该类似于以下内容:)

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}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...