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

发布于 2024-07-13 05:22:22 字数 34 浏览 5 评论 0原文

makefile 中的 -I 和 -L 标志有何用途?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧夏天 2024-07-20 05:22:22

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

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

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

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

-L$(HOME)/libs -labc

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

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

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

# 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 是最明显的)将会失败。

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 is the path to the directories containing the libraries. A search path for libraries.

-l is the name of the library you want to link to.

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

-L$(HOME)/libs -labc

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

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

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

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.

眼前雾蒙蒙 2024-07-20 05:22:22

要真正理解 makefile,您还需要充分了解项目工具链所有组件的命令行。 make 本身无法理解像 -I-L 这样的选项。 相反,make 尝试创建一个命令行,该命令行将执行一个工具将必备文件转换为目标文件。

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

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

总而言之,这些特定选项在编译器和链接器中是相当标准的。 -I 将一个目录添加到编译器搜索的位置列表中,以查找在 #include 行上命名的文件,而 -L 添加一个目录到链接器搜索的位置列表,以查找使用 -l 选项命名的库。

最重要的是,makefile 的“语言”是 makefile 本身语法、make 已知的 shell(通常是 /bin/sh 或类似的东西)、常见 shell 命令的组合(例如 rmcpinstall 等),以及特定于您的编译器和链接器的命令(例如键入 gcc shell 提示符处的 -v --help 将为您提供一个几乎完整(且非常长)的选项列表,其中 gcc 可以将其理解为一个起点)。

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 -I and -L are 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.

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

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. -I adds a directory to the list of places searched by the compiler for a file named on a #include line, and -L adds a directory to the list of places searched by the linker for a library named with the -l option.

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/sh or 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 --help at your shell prompt will give you a nearly complete (and extremely long) list of the options understood by gcc as one starting point).

会傲 2024-07-20 05:22:22

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文