环境:
- centos7
- 手动升级gcc-4.9.0
- PATH环境变量
[root@S21759 log4cplus]# echo $PATH
/usr/local/gcc-4.9.0/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/cmake-3.15.4/bin/
编译log4cplus静态库,并使用
编译命令:
./configure CXXFLAGS="-fPIC" --prefix=$(pwd)/../__INSTALL --enable-static
make && make install
应用CMakeLists.txt中不加
SET(CMAKE_CXX_COMPILER "g++")
报如下错误:
../../../../SDK/Lib/liblog4cplus.a(liblog4cplus_la-fileappender.o): In function `log4cplus::preprocessFilenamePattern(std::string const&, log4cplus::DailyRollingFileSchedule&)':
fileappender.cxx:(.text+0x7c08): undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)'
../../../../SDK/Lib/liblog4cplus.a(liblog4cplus_la-patternlayout.o): In function `log4cplus::pattern::LoggerPatternConverter::convert(std::string&, log4cplus::spi::InternalLoggingEvent const&)':
patternlayout.cxx:(.text+0x304): undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)'
../../../../SDK/Lib/liblog4cplus.a(liblog4cplus_la-patternlayout.o): In function `log4cplus::pattern::BasicPatternConverter::convert(std::string&, log4cplus::spi::InternalLoggingEvent const&)':
patternlayout.cxx:(.text+0xaac): undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)'
../../../../SDK/Lib/liblog4cplus.a(liblog4cplus_la-patternlayout.o): In function `log4cplus::pattern::PatternConverter::formatAndAppend(std::ostream&, log4cplus::spi::InternalLoggingEvent const&)':
patternlayout.cxx:(.text+0xe1e): undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)'
../../../../SDK/Lib/liblog4cplus.a(liblog4cplus_la-patternlayout.o): In function `log4cplus::pattern::PatternParser::extractOption()':
patternlayout.cxx:(.text+0x200a): undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)'
../../../../SDK/Lib/liblog4cplus.a(liblog4cplus_la-property.o):property.cxx:(.text+0x10c4): more undefined references to `std::__throw_out_of_range_fmt(char const*, ...)' follow
collect2: error: ld returned 1 exit status
加上后编译正常
总结:
CMakeLists中使用指定版本的编译器需要满足如下条件:
- 在环境变量中可以找到gcc和g++的执行目录,及PATH环境变量中
- 在CMakeLists中,添加如下设置
SET(CMAKE_CXX_COMPILER “g++”)
SET(CMAKE_C_COMPILER “gcc”)