Windows下Boost 1.88 Cobalt模块LNK2019未解析外部符号问题
Boost Cobalt模块LNK2019未解析外部符号问题解决
背景与初始配置
- Windows系统下通过
bootstrap.bat和b2.exe cxxstd=20编译Boost 1.88,编译输出显示已构建cobalt模块并生成对应的*.lib文件。 - 初始
CMakeLists.txt配置如下:
find_package(Boost REQUIRED context system coroutine regex url PATHS "${BOOST_DIR}/bin/x64/lib/cmake") target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES} Threads::Threads)
- 注意到
Boost_LIBRARIES的值为Boost::contextBoost::systemBoost::coroutineBoost::regexBoost::url,无空格分隔但仍能正常工作,对此存在疑问。 - 根据
b2.exe的输出提示,将G:\SoftwareDev\libs\boost\stage\lib添加到链接器库路径,在CMakeLists.txt中新增:link_directories("{BOOST_DIR}/stage/lib")。
出现的链接错误
编译项目时触发LNK2019未解析外部符号错误:
[...]\out\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "class std::pmr::memory_resource * __cdecl boost::cobalt::this_thread::set_default_resource(class std::pmr::memory_resource &)" (?set_default_resource@this_thread@cobalt@boost@@YAPEAVmemory_resource@pmr@std@@PEAV456@@Z) referenced in function "public: void __cdecl `int __cdecl main(int,char * * const)'::`2'::reset_res::operator()(class std::pmr::memory_resource &)" (??Rreset_res@?1??main@@YAHHQEAPEAD@Z@QEAAXPEAVmemory_resource@pmr@std@@@Z) [...]\out\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: struct std::suspend_never __cdecl boost::cobalt::detail::main_promise::final_suspend(void)" (?final_suspend@main_promise@detail@cobalt@boost@@QEAA?AUsuspend_never@std@@XZ) referenced in function "class boost::cobalt::main __cdecl co_main$_ResumeCoro$1(int,char * * const)" (?co_main$_ResumeCoro$1@@YA?AVmain@cobalt@boost@@HQEAPEAD@Z) [...]\out\build\x64-Debug\main.cpp.obj : error LNK2019: unresolved external symbol "public: static int __cdecl boost::cobalt::detail::main_promise::run_main(class boost::cobalt::main)" (?run_main@main_promise@detail@cobalt@boost@@SAHVmain@34@@Z) referenced in function main [...]\out\build\x64-Debug\boost_beast_redirect.exe : fatal error LNK1120: 3 unresolved externals
已知同类问题的常规解决方案是用C++20编译Boost,但已按此要求编译仍出现错误。
解决方案尝试与最终结论
- 仅配置原有非cobalt依赖项时,项目可正常构建:
find_package(Boost REQUIRED context system coroutine regex url PATHS "${BOOST_DIR}/bin/x64/lib/cmake")
- 新增单独的cobalt依赖配置时,出现文件找不到错误(提示
cannot open file 'libboost_url-vc143-mt-gd-x64-1_88.lib'):
find_package(Boost REQUIRED context system coroutine regex url PATHS "${BOOST_DIR}/bin/x64/lib/cmake") find_package(Boost REQUIRED cobalt PATHS "${BOOST_DIR}/stage/lib/cmake")
- 最终可行配置:将所有Boost依赖项(包括cobalt)合并到同一条
find_package指令中,统一使用stage路径:
find_package(Boost REQUIRED context system coroutine regex url cobalt PATHS "${BOOST_DIR}/stage/lib/cmake")
内容的提问来源于stack exchange,提问作者user2366975
相关产品推荐
相关产品推荐

