You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

多模块使用boost::asio::co_spawn在O2/O3优化下触发链接错误

Boost.Asio多模块使用co_spawn开启O2/O3优化时链接错误

问题描述

在多个模块中使用co_spawn并开启O2或O3优化级别时,链接阶段出现错误,表现为编译器丢弃了部分它认定为未使用的符号。使用O0无优化级别时链接正常,用nm命令查看两个目标文件的co_spawn相关符号,导出内容一致。

最小复现代码

ctx.h

#pragma once
#include <boost/asio/io_context.hpp>

extern boost::asio::io_context io_context_;

ctx.cpp

#include "ctx.h"
boost::asio::io_context io_context_;
#pragma once
#include <boost/asio/awaitable.hpp>
#include "ctx.h"

boost::asio::awaitable<int> test_link1();

boost::asio::awaitable<void> test_link();
#include "test_link.h"

#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>

boost::asio::awaitable<int> test_link1() { co_return 0; }

boost::asio::awaitable<void> test_link() {
  boost::asio::co_spawn(io_context_, test_link1(), boost::asio::detached);
  co_return;
}

test_linka.h

#pragma once
#include <boost/asio/awaitable.hpp>

boost::asio::awaitable<int> test_linka1();

boost::asio::awaitable<void> test_linka();

test_linka.cpp

#include "test_linka.h"

#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>

#include "ctx.h"

boost::asio::awaitable<int> test_linka1() { co_return 0; }

boost::asio::awaitable<void> test_linka() {
  boost::asio::co_spawn(io_context_, test_linka1(), boost::asio::detached);
  co_return;
}

main.cpp

#include <boost/asio/co_spawn.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/signal_set.hpp>

#include "ctx.h"
#include "test_link.h"
#include "test_linka.h"

int main() {
  try {
    boost::asio::signal_set signals(io_context_, SIGINT, SIGTERM);
    signals.async_wait([&](auto, auto) { io_context_.stop(); });

    boost::asio::co_spawn(io_context_, test_link(), boost::asio::detached);
    boost::asio::co_spawn(io_context_, test_linka(), boost::asio::detached);

    io_context_.run();
  } catch (std::exception& e) {
    // exception
  }
  return 0;
}

开发环境

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/gcc-toolset-13/root/usr/libexec/gcc/x86_64-redhat-linux/13/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/gcc-toolset-13/root/usr --mandir=/opt/rh/gcc-toolset-13/root/usr/share/man --infodir=/opt/rh/gcc-toolset-13/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-libstdcxx-backtrace --with-libstdcxx-zoneinfo=/opt/rh/gcc-toolset-13/root/usr/share/zoneinfo --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-13.1.1-20230614/obj-x64-redhat-linux/isl-install --enable-offload-targets=nvptx-none --without-cuda-driver --enable-offload-defaulted --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 13.1.1 20230614 (Red Hat 13.1.1-4) (GCC) 

编译命令

g++ -c -x c++ /home/duanyu/projects/asio/ctx.cpp -I "/home/duanyu/sk-server-base/include" -g1 -o "/home/duanyu/projects/asio/obj/x64/Release/ctx.o" -Wall -Wswitch -W"no-deprecated-declarations" -W"empty-body" -Wconversion -W"return-type" -Wparentheses -W"no-format" -Wuninitialized -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -O3 -fno-strict-aliasing -fomit-frame-pointer -DBOOST_ASIO_HAS_STD_COROUTINE -DBOOST_ASIO_HAS_CO_AWAIT -DNDEBUG -DNDEBUG -fthreadsafe-statics -fexceptions -frtti -std=c++20 -fno-inline
g++ -c -x c++ /home/duanyu/projects/asio/main.cpp -I "/home/duanyu/sk-server-base/include" -g1 -o "/home/duanyu/projects/asio/obj/x64/Release/main.o" -Wall -Wswitch -W"no-deprecated-declarations" -W"empty-body" -Wconversion -W"return-type" -Wparentheses -W"no-format" -Wuninitialized -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -O3 -fno-strict-aliasing -fomit-frame-pointer -DBOOST_ASIO_HAS_STD_COROUTINE -DBOOST_ASIO_HAS_CO_AWAIT -DNDEBUG -DNDEBUG -fthreadsafe-statics -fexceptions -frtti -std=c++20 -fno-inline
g++ -c -x c++ /home/duanyu/projects/asio/test_link.cpp -I "/home/duanyu/sk-server-base/include" -g1 -o "/home/duanyu/projects/asio/obj/x64/Release/test_link.o" -Wall -Wswitch -W"no-deprecated-declarations" -W"empty-body" -Wconversion -W"return-type" -Wparentheses -W"no-format" -Wuninitialized -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -O3 -fno-strict-aliasing -fomit-frame-pointer -DBOOST_ASIO_HAS_STD_COROUTINE -DBOOST_ASIO_HAS_CO_AWAIT -DNDEBUG -DNDEBUG -fthreadsafe-statics -fexceptions -frtti -std=c++20 -fno-inline
g++ -c -x c++ /home/duanyu/projects/asio/test_linka.cpp -I "/home/duanyu/sk-server-base/include" -g1 -o "/home/duanyu/projects/asio/obj/x64/Release/test_linka.o" -Wall -Wswitch -W"no-deprecated-declarations" -W"empty-body" -Wconversion -W"return-type" -Wparentheses -W"no-format" -Wuninitialized -W"unreachable-code" -W"unused-function" -W"unused-value" -W"unused-variable" -O3 -fno-strict-aliasing -fomit-frame-pointer -DBOOST_ASIO_HAS_STD_COROUTINE -DBOOST_ASIO_HAS_CO_AWAIT -DNDEBUG -DNDEBUG -fthreadsafe-statics -fexceptions -frtti -std=c++20 -fno-inline

链接命令

g++ -o "/home/duanyu/projects/asio/bin/x64/Release/asio.out" -Wl,--no-undefined -Wl,-L"/home/duanyu/sk-server-base/lib/boost/release" -Wl,-L"/home/duanyu/sk-server-base/lib/tclog/release" -Wl,-L"/home/duanyu/sk-server-base/lib/log4cplus/release" -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack /home/duanyu/projects/asio/obj/x64/Release/ctx.o /home/duanyu/projects/asio/obj/x64/Release/main.o /home/duanyu/projects/asio/obj/x64/Release/test_link.o /home/duanyu/projects/asio/obj/x64/Release/test_linka.o -llog4cplus -ltclog -lboost_system -lboost_thread -lboost_date_time -lboost_regex -lboost_serialization -lboost_context -lpthread

错误信息

`_ZN5boost4asio6detail20co_spawn_entry_pointEPZNS1_20co_spawn_entry_pointIiNS1_16detached_handlerENS0_15any_io_executorENS1_21awaitable_as_functionIiS4_EEEENS0_9awaitableINS1_28awaitable_thread_entry_pointET1_EEPNS7_IT_S9_EENS1_14co_spawn_stateIT0_S9_T2_vEEE233_ZN5boost4asio6detail20co_spawn_entry_pointIiNS1_16detached_handlerENS0_15any_io_executorENS1_21awaitable_as_functionIiS4_EEEENS0_9awaitableINS1_28awaitable_thread_entry_pointET1_EEPNS7_IT_S9_EENS1_14co_spawn_stateIT0_S9_T2_vEE.Frame.destroy' referenced in section `.rodata.cst8' of /home/duanyu/projects/asio/obj/x64/Release/test_linka.o: defined in discarded section `.text._ZN5boost4asio6detail20co_spawn_entry_pointEPZNS1_20co_spawn_entry_pointIiNS1_16detached_handlerENS0_15any_io_executorENS1_21awaitable_as_functionIiS4_EEEENS0_9awaitableINS1_28awaitable_thread_entry_pointET1_EEPNS7_IT_S9_EENS1_14co_spawn_stateIT0_S9_T2_vEEE233_ZN5boost4asio6detail20co_spawn_entry_pointIiNS1_16detached_handlerENS0_15any_io_executorENS1_21awaitable_as_functionIiS4_EEEENS0_9awaitableINS1_28awaitable_thread_entry_pointET1_EEPNS7_IT_S9_EENS1_14co_spawn_stateIT0_S9_T2_vEE.Frame.destroy[_ZN5boost4asio6detail20co_spawn_entry_pointIiNS1_16detached_handlerENS0_15any_io_executorENS1_21awaitable_as_functionIiS4_EEEENS0_9awaitableINS1_28awaitable_thread_entry_pointET1_EEPNS7_IT_S9_EENS1_14co_spawn_stateIT0_S9_T2_vEE]' of /home/duanyu/projects/asio/obj/x64/Release/test_linka.o

补充信息

  • Boost版本:1.86.0或1.84.0
  • 操作系统:Alibaba Cloud Linux 3
  • 尝试过@sehe提供的脚本,问题仍存在,推测可能是系统问题,计划切换到CentOS测试。

内容的提问来源于stack exchange,提问作者Yu Duan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 18:47:04