1

I try to compile a boost::asio example but I get some "... undefined reference to ..." errors during Linker task.

I'am new to boost-library and spend some days to find a solution but nothings works, so please help.

  • OS: Windows 7 Pro 64-Bit
  • IDE: Eclipse IDE for C/C++ Developers, Version: 2019-03 (4.11.0), 64-Bit
  • Toolchain: MSYS2\MinGW-w64 (msys64\mingw64\bin\mingw32-make.exe)
  • Library: Boost 1.67.0 (bootstrap mingw)

Code: https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/example/cpp11/echo/blocking_udp_echo_server.cpp

//
// 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 Build Console results:

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)


berndgz
  • 11
  • 2
  • 1
    I guess you have to link your application against `ws2_32.lib` library. – vahancho May 10 '19 at 08:23
  • Big THX @vahancho, linking against "msys64\mingw64\x86_64-w64-mingw32\lib\libws2_32.a" solve the problem! – berndgz May 10 '19 at 10:06
  • The information can be found hidden in the macro BOOST_ASIO_NO_DEFAULT_LINKED_LIBS in the "Using Boost.Asio" documentation under [link](https://www.boost.org/doc/libs/1_67_0/doc/html/boost_asio/using.html) and by the way, the macro does not work! – berndgz May 10 '19 at 10:12
  • @sehe sorry, I can't find duplication of "boost" or "ws2_32.lib" content in the referenced thread. – berndgz May 10 '19 at 10:29
  • Koan: Frank: "Doctor, I can't read my name anymore" // D: "You need glasses" // "Frank: But no store sells glasses to read 'Frank' with" / D: "The name is not important" Morale: You need to solve the unresolved symbols. It doesn't matter that your particular symbol has a different name. It's just an unresolved symbol, and you resolve it by linking the definition, e.g. from a library. The linked answers go into excruciating detail on all the subtler things that could play in particular scenarios (compiler flags, ABI, linker order etc. – sehe May 10 '19 at 13:29

0 Answers0