将蚊子库链接到Linux上的Hello.c程序
我正在尝试使用我的自定义C程序编译蚊子库。因此,我所做的是写一个hello.c文件,git从下面的存储库中克隆了最新的蚊子库:
https://github.com/eclipse/mosquitto.git
并将其与下面的make命令一起编译:
make
我必须删除DOC目标,因为它要求一些依赖库。我在这台机器上没有管理权,因此不想被任何依赖性lib阻止。汇编之后,我拥有的内容是:
src/mosquitto
./lib/libmosquitto.so.1
我复制了libmosquitto.so.1将lib共享到一个名为〜/hello/:
~/hello$ cp ~/mosquitto/lib/libmosquitto.so.1 .
然后写下一个hello.c inside〜/hello/hello/的本地文件夹,该文件如下:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
我可以编译Hello.c并按照下面的方式运行它:
gcc -o hello hello.c
./hello
Hello World
但是,如果我尝试将二进制文件与蚊子库链接起来,我会收到一个错误,如下所示:
gcc -o hello hello.c -lmosquitto
/usr/bin/ld: cannot find -lmosquitto
collect2: error: ld returned 1 exit status
libmosquitto.so.1.1居住在与hello.c的同一文件夹中。我不想安装蚊子库,而是想保留在本地文件夹中并能够链接它。我还尝试了以下内容,希望-L。可以将链接指向共享LIB文件的当前目录,但仍然会遇到相同的错误:
gcc -o hello hello.c -L. -lmosquitto
/usr/bin/ld: cannot find -lmosquitto
collect2: error: ld returned 1 exit status
我的最终目标是跨编译ARM目标的库。因此,真的需要了解共享库的链接如何失败,以便在交叉编译和链接目标时可以使用相同的体验。目前,我正在x86平台上这样做。 有人可以帮忙吗?
I am trying to compile the mosquitto library with my custom c program. So WHat I have done is wrote a hello.c file, git cloned the latest mosquitto library from the below repository:
https://github.com/eclipse/mosquitto.git
and compiled it with the make command as below:
make
I had to remove the doc target as it was asking for some dependancy library. I don't have admin rights on this machine, hence don't want to be blocked by any dependancy lib. After the compilation what I have is the below:
src/mosquitto
./lib/libmosquitto.so.1
The I copied the libmosquitto.so.1 shared lib into a local folder called ~/hello/:
~/hello$ cp ~/mosquitto/lib/libmosquitto.so.1 .
then wrote a hello.c inside ~/hello/ which is as below:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
I can compile the hello.c and run it as below:
gcc -o hello hello.c
./hello
Hello World
But if I try to link the binary with the mosquitto library I get an error like the below:
gcc -o hello hello.c -lmosquitto
/usr/bin/ld: cannot find -lmosquitto
collect2: error: ld returned 1 exit status
The libmosquitto.so.1 lives in the same folder as the hello.c. I don't want to install the mosquitto library, rather would like to keep in a local folder and be able to link it. I have also tried the below with the hope that the -L. would point the linker to the present directory for the shared lib file but still get the same error:
gcc -o hello hello.c -L. -lmosquitto
/usr/bin/ld: cannot find -lmosquitto
collect2: error: ld returned 1 exit status
My ultimate objective is to cross compile the library for an arm target. So really need to understand how the linking of the shared library is failing so that I can use the same experience while cross compiling and link for the target. At the moment I am doing this on a x86 platform.
Can anyone please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
链接器不寻找
libmosquitto.so.so.1
- 它只寻找libmosquitto.a < /code> 或
libmosquitto.so
。解决方案:
ln -s libmosquitto.so.s.1 libmosquitto.so
此处的问题是运行时加载程序不在当前目录中查看对于
libmosquitto.so.1
- 它仅在系统配置的目录中查看。您可以通过添加
export ld_library_path = $ home/home/squitto/lib
来解决此问题,但这是次优的 - 您的二进制文件将起作用或不取决于环境。一个更好的解决方案是这样更改您的链接命令:
The linker doesn't look for
libmosquitto.so.1
-- it only looks forlibmosquitto.a
orlibmosquitto.so
.Solution:
ln -s libmosquitto.so.1 libmosquitto.so
The problem here is that the runtime loader doesn't look in the current directory for
libmosquitto.so.1
-- it only looks in system-configured directories.You could fix this by adding
export LD_LIBRARY_PATH=$HOME/mosquitto/lib
, but this is suboptimal -- your binary will work or not depending on the environment.A better solution is to change your link command like so: