2

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?

DarthVader
  • 52,984
  • 76
  • 209
  • 300

2 Answers2

1

If you are linking from a non-standard location, the loader won't find the library. You have several options:

  1. Inform it on a case-by-case basis: LD_LIBRARY_PATH=/usr/local/lib ./aout

  2. Hard-code the path into the executable: Add -Wl,-r,/usr/local/lib to the linker command.

  3. 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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

I had a similar error with a different program.

But adding this line to the .bashrc file in home directory solved it. (Activates and persists through relogging)

export LD_LIBRARY_PATH=path/to/log4cpp/lib:$LD_LIBRARY_PATH
Heine Nørby
  • 61
  • 1
  • 2