我正在尝试使用 Bazel 通过 boost.asio 构建简单的代码:
主.cpp
#include <boost/asio.hpp>
#include <boost/asio/spawn.hpp>
boost::asio::io_service io_service;
void listenUDP(boost::asio::yield_context ctx) {
boost::asio::ip::udp::endpoint ep(boost::asio::ip::udp::v4(), 1234);
boost::asio::ip::udp::socket sock(io_service);
char data[128];
for (;;)
{
std::size_t size = sock.async_receive(boost::asio::buffer(data, sizeof(data)), ctx);
sock.async_send(boost::asio::buffer(data, size), ctx);
}
}
int main() {
boost::asio::spawn(io_service, &listenUDP);
io_service.run();
return 0;
}
模块.bazel
bazel_dep(name = "boost.asio", version = "1.83.0")
建造
cc_binary(
name = "server",
srcs = ["main.cpp"],
deps = ["@boost.asio"]
)
我在链接过程中收到错误:
ld: Undefined symbols:
boost::coroutines::stack_traits::default_size(), referenced from:
_main in main.o
boost::coroutines::stack_traits::is_unbounded(), referenced from:
boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>::allocate(boost::coroutines::stack_context&, unsigned long) in main.o
boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>::deallocate(boost::coroutines::stack_context&) in main.o
boost::coroutines::stack_traits::maximum_size(), referenced from:
boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>::allocate(boost::coroutines::stack_context&, unsigned long) in main.o
boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>::deallocate(boost::coroutines::stack_context&) in main.o
boost::coroutines::stack_traits::minimum_size(), referenced from:
boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>::allocate(boost::coroutines::stack_context&, unsigned long) in main.o
boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>::deallocate(boost::coroutines::stack_context&) in main.o
boost::coroutines::detail::coroutine_context::jump(boost::coroutines::detail::coroutine_context&, void*), referenced from:
boost::coroutines::detail::pull_coroutine_impl<void>::pull() in main.o
boost::coroutines::detail::pull_coroutine_object<boost::coroutines::push_coroutine<void>, void, boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>, boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>>::run() in main.o
boost::coroutines::detail::push_coroutine_impl<void>::push() in main.o
boost::coroutines::detail::pull_coroutine_object<boost::coroutines::push_coroutine<void>, void, boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>, boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>>::deallocate_(boost::coroutines::detail::pull_coroutine_object<boost::coroutines::push_coroutine<void>, void, boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>, boost::coroutines::basic_standard_stack_allocator<boost::coroutines::stack_traits>>*) in main.o
boost::coroutines::detail::coroutine_context::coroutine_context(void (*)(boost::context::detail::transfer_t), boost::coroutines::detail::preallocated const&), referenced from:
boost::coroutines::pull_coroutine<void>::pull_coroutine<boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>>(boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>&&, boost::coroutines::attributes const&) in main.o
boost::coroutines::detail::coroutine_context::coroutine_context(), referenced from:
boost::coroutines::pull_coroutine<void>::pull_coroutine<boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>>(boost::asio::detail::spawned_coroutine_thread::entry_point<boost::asio::detail::old_spawn_entry_point<boost::asio::strand<boost::asio::io_context::basic_executor_type<std::__1::allocator<void>, 0ul>>, void (*)(boost::asio::basic_yield_context<boost::asio::any_io_executor>), void (*)()>>&&, boost::coroutines::attributes const&) in main.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我做错了什么? Boost 由 bazel_dep 安装,我不手动安装它或添加任何额外的链接器选项
设备:Macbook M1 MAX (ARM)
尝试编译但收到错误
Bazel 中央注册表中
@boost.asio
的各种依赖关系已被破坏。您需要在 MODULE.bazel
: 中使用额外的行来强制使用它们的最新版本
bazel_dep(name = "boost.coroutine", version = "1.83.0.bcr.2")
bazel_dep(name = "boost.context", version = "1.83.0.bcr.2")