将 libpng 与 g++ 链接
我正在尝试让 libpng 在 Linux 上工作。我无法让它通过 netbeans 工作,所以我直接运行 g++ g++ -lpng -lz main.cpp -o test
并编译。当我尝试运行它时,它会输出 ./test: error while loading共享库: libpng14.so.14: Cannot open shared object file: No such file or directory
。我认为这意味着我正在动态链接,并且找不到 .so 文件
~/Programs/NetBeansProjects/DiamondSquare$ ldd test linux-gate.so.1 => (0x008a5000) libpng14.so.14 =>未找到 libz.so.1 => /usr/local/lib/libz.so.1 (0x00209000) libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x0094b000) libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0x00e3a000) libgcc_s.so.1 =>; /lib/libgcc_s.so.1 (0x00927000) libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00220000) /lib/ld-linux.so.2 (0x00b85000)
我一开始并不想动态链接。我该如何解决这个问题?
我知道 libpng14.so.14 也位于 /usr/local/lib 中,如果这很重要的话。
如果您能告诉我如何在 netbeans 中执行此操作,则加分。
I'm trying to get libpng working on linux. I couldn't get it to work through netbeans, so I ran g++ directly as g++ -lpng -lz main.cpp -o test
and it compiles. When I try to run it it it outputs ./test: error while loading shared libraries: libpng14.so.14: cannot open shared object file: No such file or directory
. I assume this means I am linking dynamically and it can't find the .so file
~/Programs/NetBeansProjects/DiamondSquare$ ldd test
linux-gate.so.1 => (0x008a5000)
libpng14.so.14 => not found
libz.so.1 => /usr/local/lib/libz.so.1 (0x00209000)
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x0094b000)
libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0x00e3a000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00927000)
libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00220000)
/lib/ld-linux.so.2 (0x00b85000)
I didn't really want to link dynamically in the first place. How could I resolve this?
I know that libpng14.so.14 is in /usr/local/lib also, if that matters.
Extra points if you can tell me how to do this within netbeans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
奇怪的是,g++ 能够找到该库,但 test 却找不到(您可以看出 g++ 可以找到它,因为 test 特别期望 libpn14,即使您只告诉 g++ '-lpng')。您确定没有将任何 -L 或 -R 标志传递给 g++ 吗?您的 LD_PRELOAD 或 LD_LIBRARY_PATH 环境变量是否在运行 g++ 的 shell 中设置,而不是在运行测试的 shell 中设置?您可以将 LD_PRELOAD 指向特定的共享库,告诉应用程序或 g++ 如何查找它,并且会自动搜索 LD_LIBRARY_PATH 中的任何文件夹。
另外,要静态链接 libpng,请将“-Wl,-Bstatic”放在“-lpng”之前。请注意,-Bstatic 之后的任何库都将被静态链接。您可以使用“-Wl,-Bdynamic -lfoo”切换回动态并列出更多库。
It's odd that g++ is able to find the library but test can not (you can tell that g++ can find it because test specifically expect libpn14 even though you only tell g++ '-lpng'). Are you sure you aren't passing any -L or -R flags to g++? Are your LD_PRELOAD or LD_LIBRARY_PATH environment variables set in the shell you're running g++ in but not in the shell you're running test in? You can point LD_PRELOAD at a specific shared library to tell an app or g++ how to find it, and any folders in LD_LIBRARY_PATH are automatically searched.
Also to link libpng statically put "-Wl,-Bstatic" before "-lpng." Beware, any libraries after the -Bstatic will be linked statically. You can switch back to dynamic and list some more libraries by using "-Wl,-Bdynamic -lfoo".
动态链接是默认设置,一般来说应该是首选。你说 libpng 位于 /usr/local/lib 中,你对此真的很肯定吗?它找到/usr/local/lib/libz.so.1。如果 libpng14.so.14 位于 /usr/local/lib 中,它也应该找到它。
Dynamic linking is the default and should be preferred in general. You say libpng is in /usr/local/lib, are you really positive about this? It finds /usr/local/lib/libz.so.1. If libpng14.so.14 was in /usr/local/lib, too it should find it.