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

问题描述 投票:0回答:1

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

  1. 记录器测试项目
  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 - 文件夹,因此我手动复制了它。但这并没有帮助。

netbeans undefined-reference log4cxx
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.