2016-11-26 3 views
0

私のコードをコンパイルしようとしていて、動作したくありません。gccでfloor()ceil()をmath.hインクルードと-lmでコンパイルしていません

参照が定義されていないというメッセージが表示されます。私はの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$ 

「-lm "gccとの議論。ここで

は私のメイクです:

# 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 

は私が何か他のものを実装したり、特定の何かをする必要がありますか?

+1

にリンクされています。最後に来なければなりません。 – nos

+1

'-lm'をコンパイラに渡しています。それは効果がありません。 をリンカに渡す必要があります。リンカにはそれを渡す必要があります。リンケージの順序では、 'libm'で定義された関数を呼び出すオブジェクトファイルの後に指定する必要があります。 GCCとGNU Makeとのコンパイルとリンクの基本的な理解を得る必要があります。ここには初心者のチュートリアル(https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html)があります。権威あるドキュメントについては、[こちらはGCCのマニュアルです](https://gcc.gnu.org/onlinedocs/)と[こちらはGNU Makeのマニュアルです](https://www.gnu.org/software/make/manual /make.html) –

答えて

0

私はこれからの私のメイクファイルを作り直しました:

# 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 [email protected] $< $(CFLAGS) 

tp2: $(OBJ) 
    gcc -W -Wall -o [email protected] $^ $(CFLAGS) 

フラグがあなたの-lmが間違った場所にあるエンド

関連する問題