makefile 中的 -I 和 -L 有什么区别? [英] What is the difference between -I and -L in makefile?
问题描述
makefile 中 -I 和 -L 标志的用途是什么?
What is the usage of the -I and -L flags in a makefile?
推荐答案
这些通常是链接器命令行的一部分,或者直接在目标操作中提供,或者更常见地分配给 make
将被扩展以形成链接命令的变量.在这种情况下:
These are typically part of the linker command line, and are either supplied directly in a target action, or more commonly assigned to a make
variable that will be expanded to form link command. In that case:
-L
是包含库的目录的路径.库的搜索路径.
-L
is the path to the directories containing the libraries. A search path for libraries.
-l
是您要链接的库的名称.
-l
is the name of the library you want to link to.
例如,如果您想链接到库 ~/libs/libabc.a
,您需要添加:
For instance, if you want to link to the library ~/libs/libabc.a
you'd add:
-L$(HOME)/libs -labc
要利用默认的隐式链接规则,请将这些标志添加到变量 LDFLAGS
,如
To take advantage of the default implicit rule for linking, add these flags to the variable LDFLAGS
, as in
LDFLAGS+=-L$(HOME)/libs -labc
<小时>
把LDFLAGS
和LIBS
分开是个好习惯,例如
It's a good habit to separate LDFLAGS
and LIBS
, for example
# LDFLAGS contains flags passed to the compiler for use during linking
LDFLAGS = -Wl,--hash-style=both
# LIBS contains libraries to link with
LIBS = -L$(HOME)/libs -labc
program: a.o b.o c.o
$(CC) $(LDFLAGS) $^ $(LIBS) -o $@
# or if you really want to call ld directly,
# $(LD) $(LDFLAGS:-Wl,%=%) $^ $(LIBS) -o $@
即使它可能以其他方式工作,-l...
指令也应该在引用这些符号的对象之后.如果链接以错误的顺序完成,一些优化(-Wl,--as-needed
是最明显的)将失败.
Even if it may work otherwise, the -l...
directives are supposed to go after the objects that reference those symbols. Some optimizations (-Wl,--as-needed
is the most obvious) will fail if linking is done in the wrong order.
这篇关于makefile 中的 -I 和 -L 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!