Linux makefile 中的 -I 和 -L 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/519342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:57:52  来源:igfitidea点击:

What is the difference between -I and -L in makefile?

linuxmakefilelinkerld

提问by MainID

What is the usage of the -I and -L flags in a makefile?

makefile 中 -I 和 -L 标志的用途是什么?

回答by Nathan Fellman

These are typically part of the linker command line, and are either supplied directly in a target action, or more commonly assigned to a makevariable that will be expanded to form link command. In that case:

这些通常是链接器命令行的一部分,或者直接在目标操作中提供,或者更常见地分配给make将被扩展以形成链接命令的变量。在这种情况下:

-Lis the path to the directories containing the libraries. A search path for libraries.

-L是包含库的目录的路径。库的搜索路径。

-lis the name of the library you want to link to.

-l是要链接到的库的名称。

For instance, if you want to link to the library ~/libs/libabc.ayou'd add:

例如,如果您想链接到~/libs/libabc.a您要添加的库:

-L$(HOME)/libs -labc

To take advantage of the default implicit rule for linking, add these flags to the variable LDFLAGS, as in

要利用默认的隐式链接规则,请将这些标志添加到变量中LDFLAGS,如

LDFLAGS+=-L$(HOME)/libs -labc


It's a good habit to separate LDFLAGSand LIBS, for example

LDFLAGS和分开是一个好习惯LIBS,例如

# 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 $@

Even if it may work otherwise, the -l...directives are supposed to go afterthe objects that reference those symbols. Some optimizations (-Wl,--as-neededis the most obvious) will fail if linking is done in the wrong order.

即使它可以工作,否则,该-l...指令是应该去引用这些符号的对象。-Wl,--as-needed如果链接以错误的顺序完成,一些优化(是最明显的)将失败。

回答by sateesh

One thing to note is that these are the options passed to the compiler/linker. So you should be looking at the compiler man pages/documentation to know their role.

需要注意的一件事是,这些是传递给编译器/链接器的选项。因此,您应该查看编译器手册页/文档以了解它们的作用。

回答by RBerteig

To really grok a makefile, you need to also have a good understanding of the command lines for all of the components of your project's toolchain. Options like -Iand -Lare not understood by make itself. Rather, make is attempting to create a command line that will execute a tool to transform a prerequisite file into a target file.

要真正理解 makefile,您还需要很好地理解项目工具链中所有组件的命令行。make 本身不理解像-I-L这样的选项。相反,make 试图创建一个命令行,该命令行将执行一个工具来将先决条件文件转换为目标文件。

Often, that is a C or C++ source file being compiled to an object file, and eventually linked to get an executable file.

通常,这是将 C 或 C++ 源文件编译为目标文件,并最终链接以获取可执行文件。

In that case, you need to see the manual for your compiler, and especially the bits related to the command line options it understands.

在这种情况下,您需要查看编译器的手册,尤其是与它理解的命令行选项相关的位。

All that said in generic terms, those specific options are pretty standard among compilers and linkers. -Iadds a directory to the list of places searched by the compiler for a file named on a #includeline, and -Ladds a directory to the list of places searched by the linker for a library named with the -loption.

所有这些都笼统地说,这些特定选项在编译器和链接器中是非常标准的。-I将一个目录添加到编译器搜索的位置列表中,以查找#include在行上命名的文件,并-L在链接器搜索的位置列表中添加一个目录,以查找使用-l选项命名的库。

The bottom line is that the "language" of a makefile is a combination of the syntax of the makefile itself, your shell as known to make (usually /bin/shor something similar), common shell commands (such as rm, cp, install, etc.), and the commands specific to your compiler and linker (e.g. typing gcc -v --helpat your shell prompt will give you a nearly complete (and extremely long) list of the options understood by gcc as one starting point).

底线是,一个makefile的“语言”,是生成文件本身的语法的组合,你的shell已知使(通常是/bin/sh或类似的东西),常见的shell命令(如rmcpinstall,等),和特定于您的编译器和链接器的命令(例如,gcc -v --help在您的 shell 提示符下键入将为您提供一个几乎完整的(并且非常长的)gcc 理解的选项列表作为一个起点)。