I bought a new MacBook with the ARM chip and tried starting development on a multithreaded server using boost and c++, however I get a lot of undefined symbol errors when trying to compile. Every error has the above style with something not being defined for the architecture and being inside the Thread library.
I've checked for all the headers and correct paths and tried linking in the libraries boost_thread-mt.a and/or boost_thread-mt.dylib directly but nothing works.
All other includes from the Boost collection work fine.
I am using Xcode 13.1 and Boost 1.76.0.
#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <thread>
using boost::asio::ip::tcp;
const int max_length = 1024;
typedef boost::shared_ptr<tcp::socket> socket_ptr;
void session(socket_ptr sock) {
try {
for (;;) {
char data[max_length];
boost::system::error_code error;
size_t length = sock->read_some(boost::asio::buffer(data), error);
if (error == boost::asio::error::eof)
break; // Connection closed cleanly by peer.
else if (error)
throw boost::system::system_error(error); // Some other error.
boost::asio::write(*sock, boost::asio::buffer(data, length));
}
} catch (std::exception& e) {
std::cerr << "Exception in thread: " << e.what() << "\n";
}
}
void server(boost::asio::io_service& io_service, short port) {
tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
for (;;) {
socket_ptr sock(new tcp::socket(io_service));
a.accept(*sock);
//std::thread t(boost::bind(session, sock));
boost::thread t(boost::bind(session, sock));
}
}
int main(int argc, char* argv[]) {
try {
boost::asio::io_service io_service;
server(io_service, 1234);
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
The Error:
Undefined symbols for architecture arm64:
"boost::detail::thread_data_base::~thread_data_base()", referenced from:
boost::detail::thread_data<boost::_bi::bind_t<void, void (*)(boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor> >), boost::_bi::list1<boost::_bi::value<boost::shared_ptr<boost::asio::basic_stream_socket<boost::asio::ip::tcp, boost::asio::any_io_executor> > > > > >::~thread_data() in main-ec8ee2.o
"boost::thread::start_thread_noexcept()", referenced from:
boost::thread::start_thread() in main-ec8ee2.o
"boost::thread::detach()", referenced from:
boost::thread::~thread() in main-ec8ee2.o
(It's of course a lot lot more but all the same pattern)