【问题标题】:Not compiling floor() ceil() with math.h include and -lm in gcc不在 gcc 中使用 math.h include 和 -lm 编译 floor() ceil()
【发布时间】: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 manualhere is the GNU Make manual

标签: math gcc include


【解决方案1】:

我已经从这里修改了我的makefile:

# Variables predefinies
CC = gcc
CFLAGS = -W -Wall -g
LIBS=-lm
# 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

对此:

CC=gcc
CFLAGS= -lm
DEPS = imagePGM.h
OBJ = imagePGM.o tp2.o 

%.o: %.c $(DEPS)
    $(CC) -W -Wall -c -o $@ $< $(CFLAGS)

tp2: $(OBJ)
    gcc -W -Wall -o $@ $^ $(CFLAGS)

标志在最后链接

【讨论】:

    猜你喜欢
    • 2017-02-19
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多