使用交叉编译时,代码里引用了boost库的方法,编译成功时链接报错。如下
- 代码里引用如下。使用了boost库自带的时间类,获取日期等信息
#include "boost/date_time/gregorian/gregorian.hpp"
- 编译后报错如下
undefined reference to `boost::gregorian::greg_month::as_long_string()
原因分析:
- 编译时自动引用了boost的基本库。
- gregorian.hpp里引用了boost_date_time.so。
- boost_date_time.so 不属于基本库,是组件库的能力。
- 在cmake中添加库的引用。如下有两个方案。亲测都可以。
-
方案一
#查找Boost是否安装
find_package(Boost REQUIRED COMPONENTS
date_time)
if (Boost_FOUND)
message(STATUS "找到Boost:\"${Boost_INCLUDE_DIRS}\",ENABLE_BOOST宏已打开")
message(STATUS "找到Boost:\"${Boost_LIBRARIES}\"")
include_directories(${Boost_INCLUDE_DIRS})
add_definitions(-DENABLE_BOOST)
list(APPEND LINK_LIB_LIST ${Boost_LIBRARIES})
endif (Boost_FOUND)
add_executable(xxx ${ALL_COMPILE_SRC})
target_link_libraries(xxx ${Boost_LIBRARIES})
方案二
find_package(boost_date_time REQUIRED)
set(BOOST_LIBRARIES Boost::date_time)
target_link_libraries(xxx ${BOOST_LIBRARIES})