c - -lm 不在 makefile 中链接数学库

标签 c unix gcc makefile

我知道这个错误已经被打死了,但我似乎无法让它发挥作用。我在下面链接了我的 makefile:

all: gensine info cs229towav

encode.o: encode.h encode.c
    gcc -c encode.c

write.o: write.c write.h
    gcc -c write.c

gensine.o: encode.c gensine.h gensine.c helper.c write.c
    gcc -c gensine.c -lm

helper.o: helper.c helper.h
    gcc -c helper.c

read.o: read.h read.c
    gcc -c read.c

info.o:read.c info.h info.c decode.c
    gcc -c info.c

decode.o: decode.c decode.h helper.c
    gcc -c decode.c

cs229towav.o: write.c read.c cs229towav.c cs229towav.h helper.c decode.c encode.c
    gcc -c cs229towav.c -lm

gensine: encode.o gensine.o write.o helper.o
    gcc -o gensine encode.o gensine.o write.o helper.o -lm

info: read.o info.o decode.o helper.o
    gcc read.o info.o decode.o helper.o

cs229towav: write.o read.o cs229towav.o decode.o encode.o helper.o
    gcc -o write.o read.o cs229towav.o decode.o encode.o helper.o -lm

Clean:
    rm -rf *o gensine info cs229towav

当我运行诸如“make gensine”之类的命令时,返回的结果如下:

>cc gensine.c -o gensine
/tmp/ccojm09X.o: In function `encodeCsFormat':
gensine.c:(.text+0x4b1): undefined reference to `sin'
/tmp/ccojm09X.o: In function `encodeWavFormat':
gensine.c:(.text+0xa39): undefined reference to `sin'
collect2: error: ld returned 1 exit status

读完后说是 undefined reference to sin,它在数学库中。列出的那些函数在包含在“gensine.c”文件中的“encode.c”文件中。

最佳答案

makefile中的命令:

gcc -o gensine encode.o gensine.o write.o helper.o -lm

与你最后打印的命令不匹配:

cc gensine.c -o gensine

另请注意,没有 -lm

请注意,make 知道如何生成目标文件,因此您不需要 makefile 的大部分内容。试试这个(记得用 TAB 缩进):

.PHONY : all clean
all = gensine info
CFLAGS =-Wall
LIBS = -lm

gensine: encode.o gensine.o write.o helper.o 
       gcc -o $@ $^ $(LIBS)

info: read.o info.o decode.o helper.o
       gcc -o $@ $^ $(LIBS)

cs229towav: write.o read.o cs229towav.o decode.o encode.o helper.o
       gcc -o $@ $^ $(LIBS)

clean:
       rm -rf *.o gensine info cs229towav

编辑:

Boddie,请注意,您之所以感到困惑,是因为您认为 makefile 是一个脚本 - 即。当您键入 make gensine 时,您正在运行名为 make 的脚本。事实上,make 是一个类似 gcc 的命令,位于文件系统的其他地方(在 Linux 等上,键入 which make 以查看它在哪里)。 make 命令希望在当前目录中找到包含名为 makefileMakefile 的构建规则的输入文件。如果它没有找到该文件,它会使用一些内置规则来代替 - 因此 cc gensine.c -o gensine 不在您的 makefile 中。如果需要,您可以使用 -f 开关告诉 make makefile 的名称(这样它就不会使用默认名称),如@DanielFischer 所述在评论中。

关于c - -lm 不在 makefile 中链接数学库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13326723/

相关文章:

c - 如何初始化结构体中的数组

linux - 需要修复 if else 脚本

perl - 如何在 unix 中守护任意脚本?

c - 使用指定初始值设定项时不同的 gcc 程序集

c - gcc 内联汇编错误 "operand type mismatch for mov"

c - 为什么 y 没有被取消引用?

c - 逆波兰表示法的中缀

c - 执行 Execv() 而不是 execvp()

linux - 通过 JSP 从一台服务器到另一台服务器的 Telnet 端口连接

c++ - 如何在 C 中使用 google mock?