/usr/bin/ld: 用 gcc 编译时找不到 -l<name of the library>

/usr/bin/ld: cannot find -l<name of the library> while compiling with gcc

我正在编写一个使用 paho 库的 mqtt 通信脚本。 文件 .so 存在于 /home/chaima/paho.mqtt.c/build/output 目录中。 但是当尝试使用 gcc 编译器编译代码时,出现了这个错误

/usr/bin/ld: cannot find -l/home/chaima/paho.mqtt.c/build/output

我尝试了很多解决方案,但 none 对我有用。 如果您需要更多信息,请告诉我。 提前谢谢你。

-l 开关要求 linker 使用某个库。它后面应该是库的名称或库的文件系统路径。

/home/chaima/paho.mqtt.c/build/output 是目录的路径,而不是库。

-L 开关告诉 linker 使用某个目录作为查找库的地方。在 -L/A/B/C-L/D/E/F 之后,link 用户将在目录 /A/B/C/D/E/F 中查找库。例如,使用 -L/A/B/C -L/D/E/F -l foo,linker 将查找名为 /A/B/C/foo.<i>extension</i> 的文件和 /A/B/C/foo.<i>extension</i>,其中 <i>extension</i> 是用于库的文件扩展名之一,例如 foo.afoo.so.

中的 aso

要让 link 人在 /home/chaima/paho.mqtt.c/build/output 中使用您的库,请使用 -L/home/chaima/paho.mqtt.c/build/output,然后使用 -l<i>Name0</i> -l<i>Name1</i> -l<i>Name2</i> …,其中 <i>Name0</i>, <i>Name1</i>, <i>Name2</i> ,这就是您的库的名称。您还要求 linker 通过提供完整路径和名称而不使用任何开关来使用库,如 /home/chaima/paho.mqtt.c/build/output/foo.so.

ld 命令(直接调用 linker)和 gcc 命令(将编译 link 并执行其他命令的整体命令任务)接受这些开关。以后阅读工具使用手册(也称为“man page”)或其他文档。 ld 的手册页解释了它的 -l-L 开关的作用。在 Unix 系统上,您通常可以通过执行 man ld 查看 ld 的手册页,通过执行 man gcc 查看 gcc 的手册页。当前的 GCC 文档也是 here.