Linking libjpeg library to a shared library, and l

2019-09-06 09:32发布

问题:

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.

回答1:

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


标签: c++ g++