我尝试编译boost :: asio示例,但在链接器任务执行期间出现一些“ ...未定义的引用...”错误。
我是增强图书馆的新手,花了几天的时间找到解决方案,但是没有用,所以请帮忙。
//
// blocking_udp_echo_server.cpp
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <cstdlib>
#include <iostream>
#include <boost/asio.hpp>
using boost::asio::ip::udp;
enum { max_length = 1024 };
void server(boost::asio::io_context& io_context, unsigned short port)
{
udp::socket sock(io_context, udp::endpoint(udp::v4(), port));
for (;;)
{
char data[max_length];
udp::endpoint sender_endpoint;
size_t length = sock.receive_from(
boost::asio::buffer(data, max_length), sender_endpoint);
sock.send_to(boost::asio::buffer(data, length), sender_endpoint);
}
}
int main(int argc, char* argv[])
{
try
{
if (argc != 2)
{
std::cerr << "Usage: blocking_udp_echo_server <port>\n";
return 1;
}
boost::asio::io_context io_context;
server(io_context, std::atoi(argv[1]));
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}
CDT构建控制台结果:
09:46:11 **** Build of configuration Debug for project blocking_udp_echo_server ****
mingw32-make all
Building file: ../blocking_udp_echo_server.cpp
Invoking: GCC C++ Compiler
g++ -I"C:\boost\boost_1_67_0" -O0 -g1 -Wall -c -fmessage-length=0 -MMD -MP -MF"blocking_udp_echo_server.d" -MT"blocking_udp_echo_server.o" -o "blocking_udp_echo_server.o" "../blocking_udp_echo_server.cpp"
Finished building: ../blocking_udp_echo_server.cpp
Building target: blocking_udp_echo_server.exe
Invoking: MinGW C++ Linker
g++ -L"C:\boost\boost_1_67_0\stage\lib" -o "blocking_udp_echo_server.exe" ./blocking_udp_echo_server.o -lboost_system-mgw73-mt-d-x64-1_67
./blocking_udp_echo_server.o: In function `boost::asio::detail::winsock_init_base::startup(boost::asio::detail::winsock_init_base::data&, unsigned char, unsigned char)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/winsock_init.ipp:39: undefined reference to `__imp_WSAStartup'
./blocking_udp_echo_server.o: In function `boost::asio::detail::winsock_init_base::cleanup(boost::asio::detail::winsock_init_base::data&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/winsock_init.ipp:56: undefined reference to `__imp_WSACleanup'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::clear_last_error()':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:69: undefined reference to `__imp_WSASetLastError'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::close(unsigned long long, unsigned char&, bool, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:315: undefined reference to `__imp_closesocket'
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:332: undefined reference to `__imp_ioctlsocket'
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:347: undefined reference to `__imp_closesocket'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::recvfrom(unsigned long long, _WSABUF*, unsigned long long, int, sockaddr*, unsigned long long*, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:921: undefined reference to `__imp_WSARecvFrom'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::send(unsigned long long, _WSABUF const*, unsigned long long, int, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1167: undefined reference to `__imp_WSASend'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::sendto(unsigned long long, _WSABUF const*, unsigned long long, int, sockaddr const*, unsigned long long, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1293: undefined reference to `__imp_WSASendTo'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::socket(int, int, int, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1395: undefined reference to `__imp_WSASocketW'
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1406: undefined reference to `__imp_setsockopt'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::poll_read(unsigned long long, unsigned char, int, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1834: undefined reference to `__imp_select'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::poll_write(unsigned long long, unsigned char, int, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1888: undefined reference to `__imp_select'
./blocking_udp_echo_server.o: In function `boost::asio::detail::socket_ops::host_to_network_short(unsigned short)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:3562: undefined reference to `__imp_htons'
./blocking_udp_echo_server.o: In function `unsigned long long boost::asio::detail::socket_ops::error_wrapper<unsigned long long>(unsigned long long, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:82: undefined reference to `__imp_WSAGetLastError'
./blocking_udp_echo_server.o: In function `int boost::asio::detail::socket_ops::call_bind<int>(int boost::asio::detail::socket_ops::msghdr::*, unsigned long long, sockaddr const*, unsigned long long)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:274: undefined reference to `__imp_bind'
./blocking_udp_echo_server.o: In function `int boost::asio::detail::socket_ops::error_wrapper<int>(int, boost::system::error_code&)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:82: undefined reference to `__imp_WSAGetLastError'
./blocking_udp_echo_server.o: In function `int boost::asio::detail::socket_ops::call_setsockopt<int>(int boost::asio::detail::socket_ops::msghdr::*, unsigned long long, int, int, void const*, unsigned long long)':
C:/boost/boost_1_67_0/boost/asio/detail/impl/socket_ops.ipp:1442: undefined reference to `__imp_setsockopt'
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [makefile:46: blocking_udp_echo_server.exe] Error 1
"mingw32-make all" terminated with exit code 2. Build might be incomplete.
09:46:15 Build Failed. 19 errors, 0 warnings. (took 3s.884ms)