I have the following code which is failing to compile.
#include <stdio.h>
#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/SimpleLayout.hh>
#define LOGFILE "./test.log"
int main()
{
/*Setting up Appender, layout and Category*/
log4cpp::Appender *appender = new log4cpp::FileAppender("FileAppender",LOGFILE);
log4cpp::Layout *layout = new log4cpp::SimpleLayout();
log4cpp::Category& category = log4cpp::Category::getInstance("Category");
appender->setLayout(layout);
category.setAppender(appender);
category.setPriority(log4cpp::Priority::INFO);
/*The actual logging*/
category.info("This is for tracing the flow");
category.notice("This is to notify certain events");
category.warn("This is to generate certain warnings");
}
$ g++ -I/usr/local/include/log4cpp -L/usr/local/lib/ -llog4cpp -lpthread log.cc
This compiles. But then i get the following error.
./a.out: error while loading shared libraries: liblog4cpp.so.4: cannot open shared object file: No such file or directory
I do see liblog4cpp.so.4
in /usr/local/lib folder.
How can i resolve this?
If you are linking from a non-standard location, the loader won't find the library. You have several options:
Inform it on a case-by-case basis: LD_LIBRARY_PATH=/usr/local/lib ./aout
Hard-code the path into the executable: Add -Wl,-r,/usr/local/lib
to the linker command.
Fiddle with the environment (I think you just export LD_LIBRARY_PATH
).
(A proper build environment (such as cmake
) will typically add the linker option from (2) automatically if you make it locate your libraries in non-standard location.)
Always check ldd ./a.out
if you have loading problems to check which libraries are missing.