我和-L在makefile中的区别是什么?

时间:2022-04-28 09:13:21

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

在makefile中,i和-L标志的用法是什么?

3 个解决方案

#1


75  

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:

这些通常是链接器命令行的一部分,它们要么直接在目标操作中提供,要么通常被分配给一个make变量,这些变量将被扩展成链接命令。在这种情况下:

-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.

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

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

例如,如果您想要链接到库~/libs/libabc。你会添加:

-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 LDFLAGS and 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 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.

即使它可能起作用,-l…指令应该跟随引用这些符号的对象。如果链接以错误的顺序进行,一些优化(-Wl,即需要是最明显的)将会失败。

#2


17  

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.

要真正了解makefile,您还需要对项目工具链中所有组件的命令行有一个很好的理解。像-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. -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.

用通用术语来说,这些特定的选项在编译器和连接器中是很标准的。-我将一个目录添加到编译器搜索的位置列表中,其中包含了一个#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/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).

底线是makefile的“语言”是一个makefile的语法本身,结合您的shell众所周知(通常/bin/sh或类似的东西),普通的shell命令(如rm、cp、安装等),以及特定于您的编译器和链接器命令(例如在shell提示符下输入gcc - v——帮助会给你一个近乎完整的(和非常长)的选项列表理解gcc作为一个起点)。

#3


0  

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.

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

#1


75  

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:

这些通常是链接器命令行的一部分,它们要么直接在目标操作中提供,要么通常被分配给一个make变量,这些变量将被扩展成链接命令。在这种情况下:

-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.

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

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

例如,如果您想要链接到库~/libs/libabc。你会添加:

-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 LDFLAGS and 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 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.

即使它可能起作用,-l…指令应该跟随引用这些符号的对象。如果链接以错误的顺序进行,一些优化(-Wl,即需要是最明显的)将会失败。

#2


17  

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.

要真正了解makefile,您还需要对项目工具链中所有组件的命令行有一个很好的理解。像-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. -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.

用通用术语来说,这些特定的选项在编译器和连接器中是很标准的。-我将一个目录添加到编译器搜索的位置列表中,其中包含了一个#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/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).

底线是makefile的“语言”是一个makefile的语法本身,结合您的shell众所周知(通常/bin/sh或类似的东西),普通的shell命令(如rm、cp、安装等),以及特定于您的编译器和链接器命令(例如在shell提示符下输入gcc - v——帮助会给你一个近乎完整的(和非常长)的选项列表理解gcc作为一个起点)。

#3


0  

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.

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