单独编译链接wondertrader项目中的WtRunner时遇到链接boost库出现的问题(编译路径和链接路径不一致所导致)

问题

问题如下

libWtCore.a(CtaStrategyMgr.cpp.o): in function `boost::filesystem::directory_iterator::directory_iterator(boost::filesystem::path const&, boost::filesystem::directory_options)':
undefined reference to `boost::filesystem::detail::directory_iterator_construct(boost::filesystem::directory_iterator&, boost::filesystem::path const&, unsigned int, boost::system::error_code*)'

可能的问题原因以及尝试过的方法

1.未正确链接 Boost.Filesystem 和 Boost.System 库

在cmake文件中链接Boost.Filesystem 和 Boost.System

find_package(Boost REQUIRED COMPONENTS filesystem system)
target_link_libraries(WtRunner Boost::filesystem Boost::system)

2.WtCore库可能未正确链接boost

在构建WtCore的Cmake文件中添加这一行

TARGET_LINK_LIBRARIES(WtRunner boost_filesystem boost_system)

最终解决方法

Cmake文件内容如下

INCLUDE_DIRECTORIES(${INCS})
LINK_DIRECTORIES(${LNKS})
LINK_DIRECTORIES(../WtCore/cmake-build-debug/build_/Debug/libs
				 ../WTSTools/cmake-build-debug/build_/Debug/libs
				 ../WTSUtils/cmake-build-debug/build_/Debug/libs
)

SET(LIBS
		WtCore
		WTSTools
		WTSUtils
)

IF (MSVC)
	LIST(APPEND LIBS ws2_32)
ELSE(GNUCC)
	LIST(APPEND LIBS
			dl
			pthread
			boost_filesystem
			boost_thread)
	IF(WIN32)
		LIST(APPEND LIBS
				ws2_32 iconv)
	ENDIF()
ENDIF()

ADD_EXECUTABLE(WtRunner ${SRCS} ${HDRS})
TARGET_LINK_LIBRARIES(WtRunner ${LIBS})

注意看这两行,这两个变量并没有被定义过,他们本应该在上一级的Cmake文件中被定义,而我只是为了独立编译一个小模块,所以实际上这两行代码是不生效的。

INCLUDE_DIRECTORIES(${INCS})
LINK_DIRECTORIES(${LNKS})

由于我的linux系统中存在多个boost版本,因此推测编译时使用的boost和链接时的boost并不是一个版本,因此加上下面两行:(保证编译和链接一致)

# 设置编译时的boost头文件的路径
include_directories(/usr/local/include/boost)
# 设置链接时的boost库的路径
link_directories(/usr/local/lib)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值