Use Boost lib with VSCode

Hello,

I’m currently trying to make a small program with boost included and I have problems with that.

What I am able to do :

  • Build and run exemple program (print hello world)

What I am doing from now :

  • I’m creating a C++ project with CMake
  • I add libboost1.74-dev:arm64 to devpackages
  • I am rebuilding the container and I have no errors
  • Build and run exemple program (print hello world)

Now I am trying to run the demo code with this line included :

#include <boost/asio.hpp>

And I get that :

Thank you for your help.

Hi @ClementLerm!

Boost is a large library and can be tricky to use if you are not that much familiar with CMake (it can even be tricky for those who are familiar with CMake :face_with_hand_over_mouth: )

From your error output, seems like your problem is that the compiler is not able to file the actual implementation of some functions. For example:

undefined reference to `pthread_key_create'

This usually happens when the shared object is not found.

Could you please share your CMakeLists.txt?

Also, please take a look at CMake’s documentation about Boost libraries: FindBoost — CMake 3.25.0 Documentation

Best regards,

Here my CMakeLists.txt :

image

And errors when I’m trying to run my code :

Thank you very much for your help.

Hi @ClementLerm

After some research, I found out that Boost’s Asio is a header only lib (in a comment of this StackOverflow answer). It is also on Boost.Asio’s documentation: Using Boost.Asio - 1.74.0

So, we should not use it with CMake’s target_link_libraries. We should use it only with target_include_headers.

Also, you need to link against the mandatory lib Boost.System (according to the documentation). But to do so, you will need to also install libboost-system-dev. Seems that you don’t need to actually link against Boost.DateTime.

Having said that, I could build a Boost.Asio example: doc/html/boost_asio/example/cpp11/allocation/server.cpp - 1.74.0

And here is my CMakeLists.txt (the specific example that I used also needs threads):

project(test-boost-cmake)
cmake_minimum_required(VERSION 3.16)

find_package(Boost REQUIRED COMPONENTS system)
find_package(Threads REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_include_directories(${PROJECT_NAME} PRIVATE Boost::headers)
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)
install(TARGETS test-boost-cmake DESTINATION bin)

Let me know if this helps you.

Best regards,