如何解决Makefile中的这个链接问题?
这是我的 Makefile:
DIR=..
ARG=$(QUERY_STRING)
MAIN=main
SRC_DIR=$(DIR)/src
BIN_DIR=$(DIR)/bin
INC_DIR=$(DIR)/inc
LIB_DIR=$(DIR)/lib
LIBS=markdown
all: $(MAIN) exec
$(MAIN): $(MAIN).o
$(LD) $^ -L $(LIB_DIR) -l $(LIBS) -o $(BIN_DIR)/$@
$(MAIN).o: $(SRC_DIR)/$(MAIN).c
$(CC) $^ -I $(INC_DIR) -o $@
exec:
$(BIN_DIR)/$(MAIN) $(ARG)
clean:
rm -f *.o core.* $(BIN)/$(MAIN)
它清楚地定义了我的项目是如何组织的,所以我不会解释它。它确实编译没有任何问题,但在二进制执行 $(BIN_DIR)/$(MAIN)
时出现以下错误:
../bin/main: error while loading shared libraries: rintf: cannot open shared object file: No such file or directory
make: *** [exec] Error 127
rintf
属于什么库?我也尝试链接 -lc
,但这并不能解决问题。
我的 Makefile
有问题吗?或者我应该将额外的内容链接到 $(MAIN)
吗?
预先感谢您的回复。
Here's my Makefile:
DIR=..
ARG=$(QUERY_STRING)
MAIN=main
SRC_DIR=$(DIR)/src
BIN_DIR=$(DIR)/bin
INC_DIR=$(DIR)/inc
LIB_DIR=$(DIR)/lib
LIBS=markdown
all: $(MAIN) exec
$(MAIN): $(MAIN).o
$(LD) $^ -L $(LIB_DIR) -l $(LIBS) -o $(BIN_DIR)/$@
$(MAIN).o: $(SRC_DIR)/$(MAIN).c
$(CC) $^ -I $(INC_DIR) -o $@
exec:
$(BIN_DIR)/$(MAIN) $(ARG)
clean:
rm -f *.o core.* $(BIN)/$(MAIN)
It's clearly defined how my project is organized, so I will not explain it. It does compile without any problems, but on binary execution $(BIN_DIR)/$(MAIN)
the following error appears:
../bin/main: error while loading shared libraries: rintf: cannot open shared object file: No such file or directory
make: *** [exec] Error 127
What library does rintf
belong? I tried to link -lc
too, but that doesn't solves the problem.
Is there something wrong with my Makefile
? Or should I link something extra to $(MAIN)
?
Thanks in advance for your responses.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本质上,您不应该通过直接调用 ld 来链接程序;始终使用编译器进行链接。它将各种额外参数传递给 ld 以使事情正常工作。将
$(LD)
替换为$(CC)
。不管它是否真正解决了你的问题,都要这样做。奇怪的是您没有获得错误消息中指定的共享对象的名称。
rintf() 的手册页表明它是在
;最有可能的是,您需要将数学库添加到链接行:
-lm
。我会重写你的一些 makefile:
You should essentially never link a program by invoking
ld
directly; always use your compiler to do the linking. It passes all sorts of extra arguments told
to make things work. Replace the$(LD)
with$(CC)
. Do that regardless of whether it actually fixes your problem or not.'Tis odd that you are not getting the name of the shared object specified in the error message.
This manual page for rintf() indicates that it is declared in
<math.h>
; most likely, you need to add the maths library to the link line:-lm
.I would rewrite some of your makefile:
通常,当链接器无法找到所需的共享对象(.so 文件)时,会出现此错误。我假设是 Linux 平台。
在 Linux 操作系统中,您可以使用以下方式搜索文件:
find
或locate
。如果您可以找到 .so 文件,请尝试使用 ldconfig 更新链接器缓存。如果不起作用,请检查/etc/ld.conf.d/
下的链接器配置文件以查看是否包含库路径。如果您更改了配置,请不要忘记再次更新缓存!Usually, this error appears when the linker is not able to find the needed shared object (.so file).I am assuming Linux platform.
In Linux OS, you can search for the file using:
find
, orlocate
. If you can find the .so file, try to update the linker cache usingldconfig
. If it did not work, check the linker configuration files under/etc/ld.conf.d/
to see if the library path is included. If you changed the configuration, don't forget to update the cache again!