【发布时间】:2016-11-26 07:37:34
【问题描述】:
我一直在尝试编译我的代码,但它不想工作。
它给我一个消息错误,引用未定义。
我在所有模块和主文件中都使用了 math.h :
#include <math.h>
这是 bash 屏幕输出:
bash-4.1$ make
gcc -W -Wall -lm -g -c -o imagePGM.o imagePGM.c
gcc tp2.o imagePGM.o -o tp2
imagePGM.o: In function `imageContraste':
imagePGM.c:(.text+0x1067): undefined reference to `floor'
imagePGM.c:(.text+0x10c1): undefined reference to `ceil'
imagePGM.c:(.text+0x1103): undefined reference to `floor'
imagePGM.o: In function `imageRatio':
imagePGM.c:(.text+0x1371): undefined reference to `floor'
imagePGM.c:(.text+0x13aa): undefined reference to `ceil'
imagePGM.c:(.text+0x13ce): undefined reference to `floor'
collect2: erreur: ld a retourné 1 code d'état d'exécution
make: *** [tp2] Erreur 1
bash-4.1$
我在 gcc 中使用了“-lm”参数。
这是我的生成文件:
# Variables predefinies
CC = gcc
CFLAGS = -W -Wall -lm -g
# Dependances
# Par defaut, make (sans arguments) ne se soucie que de la premiere dependance rencontree
# Aucune action par defaut ici, car gcc ne "sait" pas comment traiter ces dependances
# Dependances plus complexes : on ne peut melanger .c, .o et .h dans la meme dependance
tp2 : tp2.o imagePGM.o
tp2.o : tp2.c imagePGM.h
# $(CC) $(CFLAGS) -c tp2.c
imagePGM.o : imagePGM.c imagePGM.h
clean :
rm tp2 tp2.o imagePGM.o
我需要实现其他东西还是做一些特定的事情?
【问题讨论】:
-
你的 -lm 放错地方了。它必须在最后。
-
您将
-lm传递给编译器。那没有效果。您必须将它传递给链接器,而您不是,并且在链接序列中,它必须出现在调用libm中定义的函数的任何目标文件之后。您需要对使用 GCC 和 GNU Make 进行编译和链接有基本的了解。这里是a fairly good beginner's tutorial。对于权威文档,here is the GCC manual 和 here is the GNU Make manual