将libjpeg库链接到共享库,并且libjpeg在运行时未定义 [英] Linking libjpeg library to a shared library, and libjpeg is undefined at runtime

查看:304
本文介绍了将libjpeg库链接到共享库,并且libjpeg在运行时未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Ubuntu的jni程序中使用libjpeg。我用g ++编译我的c ++代码,并将libjpeg添加为库。我试图链接共享版本和静态版本,但他们都导致Java中的未定义符号:jpeg_std_error错误(而我的c + +代码工作正常)。我为libjpeg头文件使用了externC。

这是我的构建脚本,其中包含静态libjpeg.a(libjpeg作为libjpeg-turbo的一部分构建并重命名

  outputName = $ libDir / libMyLib.so 
g ++ -DNDEBUG -O3 -march = native -mfpmath = sse -Ofast -flto -funroll-loops -fPIC -w -shared -o $ outputName \
-I $ jdkDir / include -I $ jdkDir / include / linux -std = c ++ 11 - pthread \
-L ​​$ libDir / SEngineLibraries / libjpegTurbo151Linux -ljpeg151 \
myCode.cpp

和共享版本(安装了libjpeg-turbo deb)。

  outputName = $ libDir / libMyLib。所以
g ++ -DNDEBUG -O3 -march = native -mfpmath = sse -Ofast -flto -funroll-loops -fPIC -w -shared -o $ outputName \
-I $ jdkDir / include -I $ jdkDir / include / linux -std = c ++ 11 -pthread \
-ljpeg \
myCode.cpp

在我的Java代码中,使用libjpeg运行代码时,出现此错误

 符号查找错误:/myDir/lib/libMyLib.so:undefined symbol:jpeg_std_error 

code>

下面是可能存在的问题:无论我在构建脚本中是否包含引用libjpeg的行,.so库都有一个不变的大小。

解决方案

很奇怪,它不适用于库的静态版本,因为那样应该不是一个额外的链接jpeg库。您可以尝试以下几种选项:


  • 指定 -Wl, - no-undefined 在.so中打印未定义引用时的错误(默认情况下,当共享库具有未解析的符号时,链接程序不会失败)
  • 尝试 ldd< sharedlib> 查看共享库链接的未解析依赖关系

  • ,尝试添加RPATH: -Wl,-rpath,/ path / to / jpeglib_so_dir - 这会在共享库中插入libjpeg.so的路径,以便它可以在不在ldd路径中解析


I am trying to use libjpeg in my jni program on Ubuntu. I built my c++ code with g++, with libjpeg added as a library. I tried both linking the shared version and the static version, but both of them cause "undefined symbol: jpeg_std_error" error in Java (while my c++ codes worked fine). I did use "extern "C"" for the libjpeg header.

Here is my build script with static libjpeg.a (libjpeg built as part of libjpeg-turbo and renamed libjpeg151.a):

outputName=$libDir/libMyLib.so
g++ -DNDEBUG -O3 -march=native -mfpmath=sse -Ofast -flto -funroll-loops -fPIC -w -shared -o $outputName \
-I$jdkDir/include -I$jdkDir/include/linux -std=c++11 -pthread \
-L$libDir/SEngineLibraries/libjpegTurbo151Linux -ljpeg151 \
myCode.cpp 

and the one with shared version (with libjpeg-turbo deb installed)

outputName=$libDir/libMyLib.so
g++ -DNDEBUG -O3 -march=native -mfpmath=sse -Ofast -flto -funroll-loops -fPIC -w -shared -o $outputName \
-I$jdkDir/include -I$jdkDir/include/linux -std=c++11 -pthread \
-ljpeg \
myCode.cpp 

In my Java code, when running to codes using libjpeg, this error pops out:

symbol lookup error: /myDir/lib/libMyLib.so: undefined symbol: jpeg_std_error

Here is something might be suspicious: no matter I include the line referencing libjpeg in the build script or not, the .so library has a constant size.

解决方案

It is strange that it doesn't work with the static version of the library, because then there should not be an extra linked jpeg lib. Few options which you can try:

  • specify -Wl,--no-undefined to print error for undefined references in the .so (by default the linker doesn't fail when shared library has unresolved symbols)
  • try ldd <sharedlib> to see unresolved dependencies
  • for shared library linking, try adding RPATH: -Wl,-rpath,/path/to/jpeglib_so_dir - this inserts the path to the libjpeg.so inside the shared library so that it can be resolved without being in the ldd path

这篇关于将libjpeg库链接到共享库,并且libjpeg在运行时未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆