Log4cpp编译错误

发布于 2024-12-16 18:20:08 字数 1166 浏览 0 评论 0原文

我有以下代码无法编译。

#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

编译。但后来我收到以下错误。

./a.out: error while loading shared libraries: liblog4cpp.so.4: cannot open shared object file: No such file or directory

我确实在 /usr/local/lib 文件夹中看到 liblog4cpp.so.4 。 我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别靠近我心 2024-12-23 18:20:08

如果您从非标准位置链接,加载程序将找不到该库。您有多种选择:

  1. 根据具体情况通知它:LD_LIBRARY_PATH=/usr/local/lib ./aout

  2. 将路径硬编码到可执行文件中:添加-Wl,-r,/usr/local/lib 添加到链接器命令。

  3. 摆弄环境(我认为您只是导出LD_LIBRARY_PATH)。

(如果您使其将库定位在非标准位置,则正确的构建环境(例如 cmake)通常会自动添加 (2) 中的链接器选项。)

始终检查 ldd ./ a.out 如果您遇到加载问题,请检查缺少哪些库。

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.

清醇 2024-12-23 18:20:08

我在另一个程序中遇到了类似的错误。

但是将这一行添加到主目录中的 .bashrc 文件中解决了这个问题。 (通过重新登录激活并保持)

export LD_LIBRARY_PATH=path/to/log4cpp/lib:$LD_LIBRARY_PATH

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文