I use log4cxx logging library. I need to link with its static version to avoid additional binary dependencies. I use it in my dynamic library. Default build of log4cxx produces static library but I cannot link with it because it was compiled w/o -fPIC flag. So I changed log4cxx bulding as:
CPPFLAGS="-fPIC -static" ./configure
make
As a result I received a liblog4cxx.a that I can link with my .so library. Linking was done by Cmake, something like:
target_link_libraries(my_dynamic_lib log4cxx)
link_directories(relative_path_to_dir_where_liblog4cxx.a_lives)
Everything looked fine until runtime. I cannot load my_dynamic_lib.so because of undefined symbol "logger"
Please explain me what’s wrong and how to resolve this problem.
thanks
You can verify whether the shared library contains the symbol by using
If it is shown with symbol type
Uit means it’s undefined.Normally a shared library will not resolve all the symbols it needs until run-time, so it is possible (and perfectly normal) to link a shared library with missing symbols.
If you put
-llog4cxxat the start of the linker command line formy_dynamic_lib.sothen it won’t link to any of the code in there, and will leave theloggersymbol unresolved until run-time. To force it to use the symbols in the static library make sure you list the static library after the objects that need it:I don’t know how to do that with cmake, but it looks as though your CMakefile only links to
log4cxxwhen linking the main executable, not the dynamic library.