【发布时间】:2020-02-08 17:07:29
【问题描述】:
我正在尝试在我的项目中通过 cmake 和 vcpkg 使用 boost::asio 每次我触摸与 date_time 相关的东西(甚至是隐含的)时,我都会得到一些与公历相关的链接错误。 有人知道为什么这不是链接吗? 我是 CMake 和 vcpkg 的新手。
宿主环境
- 操作系统:Windows 10
- 编译器:MSVC++ 14.23
复制
./vcpkg install Boost
cd $MyProjectDir
mkdir build
cd build
cmake -G "Visual Studio 16 2019" -A x64 ..
cmake --build .
有问题的 C++ 代码
#include <boost/asio.hpp>
int main() {
boost::asio::io_service service;
//boost::asio::deadline_timer timer(service); // this one causes error
}
我的 CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_TOOLCHAIN_FILE "C:/Repos/vcpkg/scripts/buildsystems/vcpkg.cmake")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project(Backend)
find_package(Boost REQUIRED)
add_executable(Backend
"source/main.cpp"
)
target_link_libraries(Backend PRIVATE ${Boost_LIBRARIES})
target_include_directories(Backend PRIVATE ${Boost_INCLUDE_DIRS})
失败日志
2>------ Build started: Project: Backend, Configuration: Debug x64 ------
2>Building Custom Rule C:/Repos/fridge/backend/CMakeLists.txt
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl boost::gregorian::greg_month::greg_month(unsigned short)" (__imp_??0greg_month@gregorian@boost@@QEAA@G@Z) referenced in function "public: __cdecl boost::gregorian::date::date(enum boost::date_time::special_values)" (??0date@gregorian@boost@@QEAA@W4special_values@date_time@2@@Z)
2>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl boost::gregorian::greg_month::operator unsigned short(void)const " (__imp_??Bgreg_month@gregorian@boost@@QEBAGXZ) referenced in function "public: static unsigned int __cdecl boost::date_time::gregorian_calendar_base<struct boost::date_time::year_month_day_base<class boost::gregorian::greg_year,class boost::gregorian::greg_month,class boost::gregorian::greg_day>,unsigned int>::day_number(struct boost::date_time::year_month_day_base<class boost::gregorian::greg_year,class boost::gregorian::greg_month,class boost::gregorian::greg_day> const &)" (?day_number@?$gregorian_calendar_base@U?$year_month_day_base@Vgreg_year@gregorian@boost@@Vgreg_month@23@Vgreg_day@23@@date_time@boost@@I@date_time@boost@@SAIAEBU?$year_month_day_base@Vgreg_year@gregorian@boost@@Vgreg_month@23@Vgreg_day@23@@23@@Z)
2>C:\Repos\fridge\backend\build\Debug\Backend.exe : fatal error LNK1120: 2 unresolved externals
2>Done building project "Backend.vcxproj" -- FAILED.
【问题讨论】:
-
我认为你需要在
find_package(Boost中指定date_time -
我放弃了指定这些,因为其中一些是由 cmake 找到的,而另一些则没有,即使我安装了所有这些(所有 boost 模块)。例如写作
COMPONENTS filesystem有效,但COMPONENTS uuid或COMPONENTS date_time无效。因为 uuid cmake 说他找不到它。这就是为什么我想要整个 BoostBoost REQUIRED没有任何特定组件(在这种情况下,uuid、asio、文件系统和许多其他东西都是可用的)——不处理这个废话,我也是 Boost 的新手,想试试反正很多都是模块。 -
你不需要在里面放像 uuid 和 asio 这样的只有头文件的库
-
好的,很高兴知道:)
标签: c++ cmake linker boost-asio vcpkg