2

I have used boost/json inside my C++ project which I have created under windows. There the dependency was installed with vcpkg (vcpkg.exe install boost-json). Now I want to port this project to Ubuntu. But I have no clue how to install the library under Linux. Probably this is obvious to a C++ veteran but I couldn't get it to work. I can't find any hints in the git project or on the official website.

I have already tried:

  • using CMake to build and install the library
  • including the code via add_library in my CMakeLists.txt
  • Using it as header-only by copying only the include folder

What is the best practice to include such a library in a project and what are the steps to achieve it? Is there a tutorial for such tasks? My biggest problem is, I don`t know what to google for.

I hope someone can help me, thank you in advance.

EDIT:

As proposed by @vre, I built boost 1.78.0 from source. CMake finds now the boost version with version 1.78.0 and the include error is gone. Nevertheless it is still not working as the linking under Linux is failing. The following output I get:

/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `parse_server_config_json(std::filesystem::__cxx11::path)':
main.cpp:(.text+0x511): undefined reference to `boost::json::parse(boost::basic_string_view<char, std::char_traits<char> >, boost::json::storage_ptr, boost::json::parse_options const&)'
/usr/bin/ld: main.cpp:(.text+0x544): undefined reference to `boost::json::value::~value()'
/usr/bin/ld: main.cpp:(.text+0x589): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x5d0): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x615): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x662): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x6af): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o:main.cpp:(.text+0x758): more undefined references to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)' follow
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `parse_server_config_json(std::filesystem::__cxx11::path)':
main.cpp:(.text+0xb46): undefined reference to `boost::json::object::~object()'
/usr/bin/ld: main.cpp:(.text+0xbb4): undefined reference to `boost::json::value::~value()'
/usr/bin/ld: main.cpp:(.text+0xd4f): undefined reference to `boost::json::object::~object()'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::object::object(boost::json::object const&)':
main.cpp:(.text._ZN5boost4json6objectC2ERKS1_[_ZN5boost4json6objectC5ERKS1_]+0x4a): undefined reference to `boost::json::object::object(boost::json::object const&, boost::json::storage_ptr)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_object()':
main.cpp:(.text._ZN5boost4json5value9as_objectEv[_ZN5boost4json5value9as_objectEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_array()':
main.cpp:(.text._ZN5boost4json5value8as_arrayEv[_ZN5boost4json5value8as_arrayEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_string() const':
main.cpp:(.text._ZNK5boost4json5value9as_stringEv[_ZNK5boost4json5value9as_stringEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_int64()':
main.cpp:(.text._ZN5boost4json5value8as_int64Ev[_ZN5boost4json5value8as_int64Ev]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Server.dir/build.make:102: Server] Error 1
make[1]: *** [CMakeFiles/Makefile2:140: CMakeFiles/Server.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I also added as mentioned by @GP8:

find_package( 
 Boost 1.78 REQUIRED 
 COMPONENTS json 
)

Edit2:

I forgot to link boost-json. After adding the following to my CMakeLists.txt the build was successfull under linux:

target_link_libraries(${PROJECT_NAME}
    Boost::boost
    Boost::json
)
sebwr
  • 113
  • 1
  • 10
  • You probably just want to [use your package manager](https://stackoverflow.com/questions/12578499/how-to-install-boost-on-ubuntu) – Nathan Pierson Mar 14 '22 at 15:23
  • 3
    @NathanPierson As the latest Ubuntu (21.10) only includes boost 1.74 and Boost/JSON was introduced in 1.75 this won't help. One need to build Boost or install vcpkg (see [here](https://lindevs.com/install-vcpkg-on-ubuntu/)) on Ubuntu. – vre Mar 14 '22 at 15:52
  • @vre thanks, this is part of my problem, as I have installed libboost-all-dev on my linux system. In the Future, i have to check which version is needed for specific features. I will try to buiöd boost for myself tomorrow… – sebwr Mar 14 '22 at 19:02

3 Answers3

3

You should first install boost using the following command : sudo apt-get install libboost-all-dev

To get Boost libraries included in your project you must find the package this way :

find_package( 
 Boost 1.65 REQUIRED 
 COMPONENTS  json 
)

You can then, tell CMake to with which file to create your executable and against which libraries to link :

add_execublable( anyExecutable main.cpp )
target_link_libraries( exeLINK_PUBLIC ${Boost_LIBRARIES})
Dharman
  • 30,962
  • 25
  • 85
  • 135
GPB
  • 44
  • 2
  • As mentioned above, this should not work, as the version is not available for the current ubuntu version… Or do I miss something? Nevertheless, the second part of your answer is new to me and I Dont understand it. But I will look into it tomorrow, thanks! – sebwr Mar 14 '22 at 19:06
  • 1
    Once you installed Boost, you can get the current version numer easly using ```apt list | grep *libboost-all-dev* ``` You will have to replace the version number in my example above by the correct one ! – GPB Mar 14 '22 at 20:09
  • In the end the solution is included in this answer, but there were multiple steps for me to understand every part of the solution. – sebwr Mar 15 '22 at 14:01
3

@GPB outlines the general procedure.

If your CMake/FindBoost doesn't support Boost Json yet, the simplest thing to do is to

#include <boost/json/src.hpp>

in exactly 1 (one) translation unit that participates in your linked binary.

See Header Only

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Yes I tried that, but I was confused where to put this src.hpp... I took the file from the include folder in the GitHub repository but it did not work... Can you provide further explanations? – sebwr Mar 15 '22 at 07:44
0

A note on @GPB's solution: To make it work I needed to include the library name as follows:

target_link_libraries( exeLINK_PUBLIC ${Boost_LIBRARIES}/libboost_json.a)

Without this it didn't work, at least in my case.