Apache log4cxx 是 log4j 的 C++ 移植版,廣泛用於企業級 C++ 專案的日誌記錄。官方文檔與網上多數教程在 CentOS 6.x + GCC 4.4 下常因缺少 <string.h> 導致 memmove/memcpy 未聲明錯誤而編譯失敗。本文記錄一次完整成功的編譯安裝全過程,含關鍵補丁、環境變數配置與測試驗證。
| 作業系統 | CentOS 6.3 (Kernel 2.6.32) |
| 編譯器 | g++ (GCC) 4.4.6 |
| 目標版本 | apr-1.4.6 / apr-util-1.4.1 / apache-log4cxx-0.10.0 |
| 安裝前綴 | /usr/local/{apr,apr-util,log4cxx} |
wget https://archive.apache.org/dist/apr/apr-1.4.6.tar.gz
wget https://archive.apache.org/dist/apr/apr-util-1.4.1.tar.gz
wget https://archive.apache.org/dist/logging/log4cxx/0.10.0/apache-log4cxx-0.10.0.tar.gz
tar zxvf apr-1.4.6.tar.gz
tar zxvf apr-util-1.4.1.tar.gz
tar zxvf apache-log4cxx-0.10.0.tar.gz
cd apr-1.4.6
./configure --prefix=/usr/local/apr
make && make install
cd ..
cd apr-util-1.4.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
make && make install
cd ..
進入源碼目錄後,必須先修補三個 .cpp 文件,補上 #include <string.h> (部分還需 <stdio.h>/<stdlib.h>),否則 make 會報 memmove/memcpy/puts was not declared in this scope。
| 文件路徑 | 新增 include 行 | 原報錯符號 |
|---|---|---|
src/main/cpp/inputstreamreader.cpp | #include <string.h> | memmove |
src/main/cpp/socketoutputstream.cpp | #include <string.h> | memcpy |
src/examples/cpp/console.cpp | #include <string.h>#include <stdio.h>#include <stdlib.h> | puts |
操作示例 (以 vim 為例):
vim src/main/cpp/inputstreamreader.cpp
# 在現有 #include 區塊後插入
#include
cd apache-log4cxx-0.10.0
./configure --prefix=/usr/local/log4cxx \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util
make && make install
cd ..
動態庫路徑必須加入 LD_LIBRARY_PATH,建議寫入 /etc/profile.d/log4cxx.sh 永久生效:
echo 'export LD_LIBRARY_PATH=/usr/local/log4cxx/lib:$LD_LIBRARY_PATH' > /etc/profile.d/log4cxx.sh
source /etc/profile.d/log4cxx.sh
test_log4cxx.cpp#include
#include
#include
int main(int argc, char* argv[]) {
using namespace log4cxx;
PropertyConfigurator::configure("conf.log");
LoggerPtr logger1 = Logger::getLogger("TraceYourMama");
LoggerPtr logger2 = Logger::getLogger("Patch");
LOG4CXX_TRACE(logger1, "跟蹤");
LOG4CXX_WARN(logger1, "警告");
LOG4CXX_DEBUG(logger1, "調試");
LOG4CXX_ASSERT(logger1, false, "斷言");
LOG4CXX_FATAL(logger1, "致命");
LOG4CXX_TRACE(logger2, "跟蹤");
LOG4CXX_ERROR(logger2, "錯誤");
return 0;
}
conf.loglog4j.rootLogger=TRACE, stdout, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=./ZW.log
log4j.appender.logfile.MaxFileSize=100KB
log4j.appender.logfile.MaxBackupIndex=10
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
g++ test_log4cxx.cpp -o test_log4cxx \
-I/usr/local/log4cxx/include \
-L/usr/local/log4cxx/lib \
-llog4cxx -lapr-1 -laprutil-1
./test_log4cxx
# 控制台與 ./ZW.log 同時輸出帶時間戳、級別、logger 名的日誌
string.h:GCC 4.4 不再隱式包含 C 標準庫頭文件,C++ 代碼必須顯式 #include <cstring> 或 <string.h>。--with-apr/--with-apr-util 必須指向 apr-1-config/apu-1-config 所在前綴。LD_LIBRARY_PATH 或 /etc/ld.so.conf.d/ 配置。記錄完整編譯鏈路、關鍵補丁與驗證代碼,旨在為後續遷移/升級提供可復現的「單一事實來源」。若升級至 CentOS 7+/GCC 4.8+,可直接 yum install log4cxx-devel 避開手工編譯。
來源:永遠的朋友博客