log4cxx 未定义对“log4cxx::Logger::getLogger(char const*)”的引用

log4cxx undefined reference to `log4cxx::Logger::getLogger(char const*)'

提问人:Uwe_98 提问时间:11/11/2023 最后编辑:skomisaUwe_98 更新时间:11/11/2023 访问量:30

问:

据我说,我在 Ubuntu Netbeans 下有 2 个相同的 cpp 项目。

  1. LoggerTest项目
  2. Cpp应用2

在两者的主文件中,我都集成了以下代码:

The result of compiling the first Project is:
#include "log4cxx/logger.h"
#include "log4cxx/basicconfigurator.h"
#include "log4cxx/propertyconfigurator.h"

#include "ratio.h" //only in LoggerTestProject

#include <cstdlib>

#define LOGCONF "./cfg/log4cxx.properties"


using namespace log4cxx;
using namespace log4cxx::helpers;

//extern LoggerPtr logger; //ahaha! AOP should be useful here
LoggerPtr logger(Logger::getLogger("LoggerTestProject"));


//using namespace std;

/*
 * 
 */
int main(int argc, char** argv)
{
    int inVar1 = 0;
LOG4CXX_INFO(logger, "Begin of LoggerTestProject");

编译第一个会导致成功:

cd '/home/uwez/NetBeansProjects/LoggerTestProject'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/uwez/NetBeansProjects/LoggerTestProject'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/loggertestproject
make[2]: Entering directory '/home/uwez/NetBeansProjects/LoggerTestProject'
mkdir -p build/Debug/GNU-Linux/_ext/9bc1829f
rm -f "build/Debug/GNU-Linux/_ext/9bc1829f/ratio.o.d"
g++ -fpermissive   -c -g -I/usr/include/log4cxx -MMD -MP -MF "build/Debug/GNU-Linux/_ext/9bc1829f/ratio.o.d" -o build/Debug/GNU-Linux/_ext/9bc1829f/ratio.o /home/uwez/NetBeansProjects/LoggerTestProject/ratio.cpp
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++ -fpermissive   -c -g -I/usr/include/log4cxx -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux
g++ -fpermissive    -o dist/Debug/GNU-Linux/loggertestproject build/Debug/GNU-Linux/_ext/9bc1829f/ratio.o build/Debug/GNU-Linux/main.o -llog4cxx
make[2]: Leaving directory '/home/uwez/NetBeansProjects/LoggerTestProject'
make[1]: Leaving directory '/home/uwez/NetBeansProjects/LoggerTestProject'

否则,将第二个编译失败:

cd '/home/uwez/NetBeansProjects/CppApplication_2'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/uwez/NetBeansProjects/CppApplication_2'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/cppapplication_2
make[2]: Entering directory '/home/uwez/NetBeansProjects/CppApplication_2'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux
g++     -o dist/Debug/GNU-Linux/cppapplication_2 build/Debug/GNU-Linux/main.o 
build/Debug/GNU-Linux/main.o: In function `__static_initialization_and_destruction_0(int, int)':
/home/uwez/NetBeansProjects/CppApplication_2/main.cpp:38: undefined reference to `log4cxx::Logger::getLogger(char const*)'
build/Debug/GNU-Linux/main.o: In function `log4cxx::helpers::ObjectPtrT<log4cxx::Logger>::~ObjectPtrT()':
/usr/include/log4cxx/helpers/objectptr.h:102: undefined reference to `log4cxx::helpers::ObjectPtrBase::~ObjectPtrBase()'
build/Debug/GNU-Linux/main.o:(.data.rel.ro._ZTIN7log4cxx7helpers10ObjectPtrTINS_6LoggerEEE[_ZTIN7log4cxx7helpers10ObjectPtrTINS_6LoggerEEE]+0x10): undefined reference to `typeinfo for log4cxx::helpers::ObjectPtrBase'
collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU-Linux/cppapplication_2' failed
make[2]: *** [dist/Debug/GNU-Linux/cppapplication_2] Error 1
make[2]: Leaving directory '/home/uwez/NetBeansProjects/CppApplication_2'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/uwez/NetBeansProjects/CppApplication_2'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

有谁知道它可能是什么?包含 log4cxx.properties 文件的 /cfg - 文件夹在第二个项目中不存在,因此我手动复制了它。但这无济于事。

C++ NetBeans 未定义引用 log4cxx

评论


答:

2赞 Stephen Webb 11/11/2023 #1

问题出在第二个项目中使用的 Makefile 上。我的猜测是它没有提到 Log4cxx(例如没有 -Llog4cxx)。 log4cxx.properties 在运行程序时使用,在构建程序时不会使用。

评论

0赞 Uwe_98 11/13/2023
谢谢斯蒂芬。那么我现在该如何解决这个问题呢?我怎样才能让它有一个-Llog4cxx?
0赞 Stephen Webb 11/15/2023
您是否致力于使用 NetBeans?它对 C++ 的支持并不好。也许可以考虑使用 VSCode,或者如果你是命令行舒适的 conan+cmake - 参见 ilyas-hamadouche.medium.com/...
0赞 Uwe_98 11/27/2023 #2

在 netbeans 8.2 的菜单中,打开“文件/项目属性”。

单击 C++ 编译器并将 log4cxx 库的路径添加到包含目录。在本例中,它的 /usr/include/log4cxx,但实际上并不是构建项目所必需的。

要构建项目,绝对有必要转到链接器/库并添加 log4cxx。在浏览窗口中,我必须打开文件夹 /usr/lib32 并键入 log4cxx.so。这是生成项目所需的包含文件。