makefile中的-I和-L有什么区别? [英] What is the difference between -I and -L in makefile?

查看:918
本文介绍了makefile中的-I和-L有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


例如,分开LDFLAGSLIBS是一个好习惯


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆