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).
In CMake, you can simply say
find_package(Boost ...)andtarget_link_libraries(my-app PRIVATE Boost::system). – tttapa Jan 07 '20 at 14:40st::cout << "Socket closed." << std::endl;you have an error here ;std– Ephemeral Jan 07 '20 at 14:53