Log4cpp编译错误
我有以下代码无法编译。
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从非标准位置链接,加载程序将找不到该库。您有多种选择:
根据具体情况通知它:
LD_LIBRARY_PATH=/usr/local/lib ./aout
将路径硬编码到可执行文件中:添加
-Wl,-r,/usr/local/lib
添加到链接器命令。将
摆弄环境(我认为您只是
导出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:
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.我在另一个程序中遇到了类似的错误。
但是将这一行添加到主目录中的 .bashrc 文件中解决了这个问题。 (通过重新登录激活并保持)
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)