简体   繁体   English

-lm不在makefile中链接数学库

[英]-lm Not linking math library in makefile

I know this error has been beaten to death, but I cannot seem to get it to work. 我知道这个错误已被打死,但我似乎无法让它发挥作用。 I have linked my makefile below: 我在下面链接了我的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

When I run a command such as "make gensine" I am returned with the following result: 当我运行诸如“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

After reading this is says undefined reference to sin, which is with the math library. 阅读之后,这是对sin的未定义引用,它与数学库有关。 Those functions listed are in the "encode.c" file which are included in the "gensine.c" file. 列出的那些函数位于“encode.c”文件中,该文件包含在“gensine.c”文件中。

The command in the makefile: makefile中的命令:

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

does not match the command you printed at the end: 与最后打印的命令不匹配:

cc gensine.c -o gensine

Notice also that there is no -lm 另请注意,没有-lm

Note that make knows how to make object files so you don't need most of the makefile. 请注意, make知道如何制作目标文件,因此您不需要大部分的makefile。 Try this (remember to indent with TABs): 试试这个(记得用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

Edit: 编辑:

Boddie, note that your confusion arose because you thought the makefile was a script - ie. Boddie,请注意你的混乱是因为你认为makefile是一个脚本 - 即。 that you were running your script named make when you typed make gensine . 您在键入make gensine时运行名为make的脚本。 In fact make is a command like gcc somewhere else in the filesystem (on Linux etc, type which make to see where it is). 其实make就像是一个命令gcc其他地方的文件系统(在Linux等类型which make看看它在哪里)。 The make command expects to find an input file containing build rules called makefile or Makefile in the current directory. make命令期望在当前目录中找到包含名为makefileMakefile构建规则的输入文件。 If it doesn't find that file it uses some built-in rules instead - hence the cc gensine.c -o gensine which is nowhere in your makefile. 如果找不到该文件,则会使用一些内置规则 - 因此cc gensine.c -o gensine在makefile中无处cc gensine.c -o gensine If you want to, you can tell make the name of the makefile (so that it doesn't use the default names) with the -f switch, as @DanielFischer described in the comments. 如果您愿意,可以使用-f开关告诉make makefile的名称(以便它不使用默认名称),如注释中所述的@DanielFischer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM