如何在Makefile中使用LDFLAGS [英] How to use LDFLAGS in makefile
问题描述
我是Linux操作系统的新手.我正在尝试使用makefile编译.c
文件.数学库必须链接.我的makefile看起来像这样:
I am new to Linux OS. I am trying to compile a .c
file using a makefile. The math library has to be linked. My makefile looks like this:
CC=gcc
CFLAGS=-Wall -lm
all:client
.PHONY: clean
clean:
rm *~ *.o client
运行make
时,出现以下错误:
When I run make
, I get the following error:
"undefined reference to rint"
因此它无法链接数学库.
So it is not able to link the math library.
但是当我使用显式编译时
But when I compile explicitly using
gcc client.c -lm -o client
它可以成功编译.
那么我应该如何更改我的makefile使其起作用.我已经尝试添加LDFLAGS=-lm
.但是我遇到了同样的错误.
So how should I change my makefile such that it works. I have already tried adding LDFLAGS=-lm
. But I get the same error.
我还应该补充一点,当我运行make
时,它会扩展为
I should also add that when I run make
, it expands to
gcc -Wall -lm client.c -o client
(请注意,当我以-lm
结尾时显式运行gcc
时,它可以工作).
(notice that when I run gcc
explicitly with -lm
at the end, it works).
推荐答案
您的链接器(ld)显然不喜欢make排列GCC参数的顺序,因此您必须稍微更改Makefile:>
Your linker (ld) obviously doesn't like the order in which make arranges the GCC arguments so you'll have to change your Makefile a bit:
CC=gcc
CFLAGS=-Wall
LDFLAGS=-lm
.PHONY: all
all: client
.PHONY: clean
clean:
$(RM) *~ *.o client
OBJECTS=client.o
client: $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) -o client $(LDFLAGS)
在定义 client 目标的行中,根据需要更改$(LDFLAGS)的顺序.
In the line defining the client target change the order of $(LDFLAGS) as needed.
这篇关于如何在Makefile中使用LDFLAGS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!