0

I am trying to use the Boost library in VSCode under Windows 10 WSL2 Ubuntu 20.04. All package installs are fresh installs

I am new to c++ and library management and have tried various options but am unable to get one to work.

I installed boost using:

sudo apt-get install libboost-all-dev

I am using g++ and followed a tutorial to get it up and running with windows 10 / wsl2 / ubuntu / g++ and have it compiling/running a base 'hello world' script.

I modified my tasks.json file as per other info on google to be:

{
  "tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++ build active file",
        "command": "/usr/bin/g++",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "-I/usr/local/include",
            "-L/usr/local/lib",
            "-lboost_system",
            "${file}",
            "-o",
            "${fileDirname}/${fileBasenameNoExtension}"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
  "version": "2.0.0"
}

The linter(I know it is separate from the build) does not complain with:

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
using tcp = boost::asio::ip::tcp;
namespace websocket = boost::beast::websocket;

but when I try to build it I get:

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g -I/usr/local/include -L/usr/local/lib -lboost_system /home/laptop/cppbot/helloworld.cpp -o /home/laptop/cppbot/helloworld
/usr/bin/ld: /tmp/ccmv4tdX.o: in function `boost::asio::detail::posix_event::posix_event()':
/usr/include/boost/asio/detail/impl/posix_event.ipp:42: undefined reference to `pthread_condattr_setclock'
/usr/bin/ld: /tmp/ccmv4tdX.o: in function `boost::asio::detail::posix_thread::~posix_thread()':
/usr/include/boost/asio/detail/impl/posix_thread.ipp:35: undefined reference to `pthread_detach'
/usr/bin/ld: /tmp/ccmv4tdX.o: in function `boost::asio::detail::posix_thread::join()':
/usr/include/boost/asio/detail/impl/posix_thread.ipp:42: undefined reference to `pthread_join'
/usr/bin/ld: /tmp/ccmv4tdX.o: in function `boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)':
/usr/include/boost/asio/detail/impl/posix_thread.ipp:59: undefined reference to `pthread_create'
/usr/bin/ld: /tmp/ccmv4tdX.o: in function `boost::asio::detail::posix_signal_blocker::posix_signal_blocker()':
/usr/include/boost/asio/detail/posix_signal_blocker.hpp:43: undefined reference to `pthread_sigmask'
/usr/bin/ld: /tmp/ccmv4tdX.o: in function `boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()':
/usr/include/boost/asio/detail/posix_signal_blocker.hpp:50: undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status

It never makes it to the actual code and fails on the imports

Any help would be appreciated. Thanks

Pearl
  • 392
  • 2
  • 12
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – starball Feb 23 '23 at 01:31
  • I read that one, and there is this one: https://stackoverflow.com/questions/13863508/linking-errors-when-compiling-boost-asio-on-linux but I still do not understand how to make the link – Pearl Feb 23 '23 at 01:35

1 Answers1

1

You have to link against pthread as well. With gcc, just add -pthread to your command.

"args": [
  "-fdiagnostics-color=always",
  "-g",
  "-I/usr/local/include",
  "-L/usr/local/lib",
  "-lboost_system",
  "-pthread",
  "${file}",
  "-o",
  "${fileDirname}/${fileBasenameNoExtension}"
],

From the gcc man pages:

   -pthread
       Define additional macros required for using the POSIX threads
       library.  You should use this option consistently for both
       compilation and linking.  This option is supported on
       GNU/Linux targets, most other Unix derivatives, and also on
       x86 Cygwin and MinGW targets.
local-ninja
  • 1,198
  • 4
  • 11