谁能告诉我 -L 和 -l 是做什么的?

问题描述 投票:0回答:1

我有一个 makefile 如下,但我不太确定 -L 和 -l 的作用

all: main.cpp
   cd nodelib && make
   g++ main.cpp -o main.exe -I nodelib/inc -L nodelib/ -lnode

供参考,我的代码结构如下:

./ 
 main.cpp 
 makefile 
 nodelib/ 
   makefile 
   inc/ 
     Node.hpp 
     Set.hpp 
   src/ 
     Node.cpp 
     Set.cpp 
     makefile

src 中的 makefile 创建一个存档

libnode.a
然后将其移动到父目录
nodelib

我对

-lnode
部分特别困惑,因为在目录中没有名为
node
的文件。尝试将其更改为
-lNode
,因为源目录中的
Node
文件以大写 N 命名,但这只会使 makefile 出错。但是如果我将其改回
-lnode

,makefile 就可以正常工作
g++ main.cpp -o main.exe -I nodelib/inc -L nodelib/ -lNode
/usr/bin/ld: cannot find -lNode
collect2: error: ld returned 1 exit status
make: *** [Makefile:5: main.exe] Error 1

提前致谢,我非常感谢您的回答!

c++ linux makefile terminal
1个回答
0
投票

-l
的文档,来自GCC的
info
关于用于链接的命令行选项

-llibrary
-l library

链接时搜索名为 library 的库。 (将库作为单独参数的第二种选择仅用于 POSIX 合规性,不推荐。)

-l
选项由 GCC 直接传递给链接器。有关确切的详细信息,请参阅链接器文档。下面的一般描述适用于 GNU 链接器。

-L
的文档,来自 GNU ld 的
info

-L searchdir
--library-path=searchdir

将路径 searchdir 添加到

ld
将搜索存档库和
ld
控制脚本的路径列表。您可以多次使用此选项。目录按照在命令行中指定的顺序进行搜索。在默认目录之前搜索在命令行上指定的目录。所有
-L
选项均适用于所有
-l
选项,无论选项出现的顺序如何。

© www.soinside.com 2019 - 2024. All rights reserved.