0

I created a simple program in C++ on my RPi to test the Boost library. This is the code:

#include <iostream>
#include <boost/asio.hpp>

int main()
{
    std::cout << "Hello World!" << std::endl;
    std::cout << "Opening TCP socket on port 9999..." << std::endl;
    boost::asio::io_context io_context;
    boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 9999);
    io_context.run();
    std::cout << "Socket opened!" << std::endl;
    io_context.stop();
    st::cout << "Socket closed." << std::endl;
    return 0;
}

And this is the compile command:

g++ -lpthread -lboost_system main.cpp -o test

But when I try to compile the program I get this error:

/usr/bin/ld: /tmp/ccGWgmo2.o: in function `boost::system::error_code::error_code()':
main.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x1c): undefined reference to `boost::system::system_category()'
/usr/bin/ld: /tmp/ccGWgmo2.o: in function `boost::system::error_category::std_category::equivalent(int, std::error_condition const&) const':
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0xcc): undefined reference to `boost::system::generic_category()'
/usr/bin/ld: main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition[_ZNK5boost6system14error_category12std_category10equivalentEiRKSt15error_condition]+0x118): undefined reference to `boost::system::generic_category()'
/usr/bin/ld: /tmp/ccGWgmo2.o: in function `boost::system::error_category::std_category::equivalent(std::error_code const&, int) const':
main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0xcc): undefined reference to `boost::system::generic_category()'
/usr/bin/ld: main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0x118): undefined reference to `boost::system::generic_category()'
/usr/bin/ld: main.cpp:(.text._ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei[_ZNK5boost6system14error_category12std_category10equivalentERKSt10error_codei]+0x1fc): undefined reference to `boost::system::generic_category()'
/usr/bin/ld: /tmp/ccGWgmo2.o: in function `boost::asio::error::get_system_category()':
main.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x8): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status

I installed Boost using this command:

sudo apt install libboost-dev

How do I go around working out why this won't compile?

P.S: I added -lpthread because I was getting errors about pthread, and I added -lboost_system because I already did research on the errors (which used to be a much larger paragraph) and it got rid of most of them.

I am running g++ (Raspbian 8.3.0-6+rpil) 8.3.0 on the Raspberry Pi 4 B (4GB).

Logan
  • 3
  • 2
  • I'd strongly recommend using CMake to build your applications. Manually keeping track of dependencies and making sure they are in the correct order does not scale.
    In CMake, you can simply say find_package(Boost ...) and target_link_libraries(my-app PRIVATE Boost::system).
    – tttapa Jan 07 '20 at 14:40
  • st::cout << "Socket closed." << std::endl; you have an error here ; std – Ephemeral Jan 07 '20 at 14:53

2 Answers2

1

Install (where XX is the last version : sudo apt-cache search libboost-system1):

sudo apt-get install libboost-dev libboost-system1.XX-dev libboost-system1.XX libasio-dev 

C++

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>    

int main(){

    std::cout << "Hello World!" << std::endl;
    std::cout << "Opening TCP socket on port 9999..." << std::endl;
    // > 1.66
    boost::asio::io_context io_context;
    // < 1.66
    //io_service io_service;

    boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 9999);
    boost::asio::io_context.run();
    std::cout << "Socket opened!" << std::endl;
    boost::asio::io_context.stop();
    std::cout << "Socket closed." << std::endl;
    return 0;
}

Older g++:

g++ -lboost_system -lpthread -o test test.cpp

New g++ :

g++ -o test -lboost_system -lpthread test.cpp 

Now you can use the lib.

Ephemeral
  • 2,157
  • 1
  • 6
  • 19
1

The problem is the order of linking. You have to compile your code first (or specify your object files first), and then specify the libraries you want to link to.

g++ main.cpp -lboost_system -o test

Using this command, and after fixing a small bug in your code (st::coutstd::cout), everything compiles and links correctly.


You can simplify the build process by using CMake.

  1. Create a CMakeLists.txt file in your project folder, and add the contents below
  2. Create a build directory and run CMake in that directory: mkdir build && cd build && cmake ..
  3. Build the project by typing make
cmake_minimum_required(VERSION 3.10)
project(test)

find_package(Boost REQUIRED COMPONENTS system) # asio is part of Boost::system

add_executable(test main.cpp)
target_link_libraries(test Boost::system)
tttapa
  • 986
  • 5
  • 7