1

I got stuck in my first asio code. I have read a first part of tutorial from official web-site(https://www.boost.org/doc/libs/1_73_0/doc/html/boost_asio/tutorial/tuttimer1.html)

Here is the code

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

    int main()
    {
        boost::asio::io_context io;
        boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
        t.wait();
        std::cout << "First app with asio!" << std::endl;
    
        return 0;
    }

And here are errors

egor@PC:~/Documents/Programming/boost_asio_practice$ g++ trial.cpp
trial.cpp: In function ‘int main()’:
trial.cpp:6:15: error: ‘io_context’ is not a member of ‘boost::asio’
  boost::asio::io_context io;
               ^~~~~~~~~~
trial.cpp:6:15: note: suggested alternative: ‘connect’
  boost::asio::io_context io;
               ^~~~~~~~~~
               connect
trial.cpp:7:15: error: ‘steady_timer’ is not a member of ‘boost::asio’
  boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
               ^~~~~~~~~~~~
trial.cpp:7:15: note: suggested alternative: ‘deadline_timer’
  boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
               ^~~~~~~~~~~~
               deadline_timer
trial.cpp:8:2: error: ‘t’ was not declared in this scope
  t.wait();
  ^
trial.cpp:8:2: note: suggested alternative: ‘tm’
  t.wait();
  ^
  tm

UPD: I have installed the latest version of boost. Now I am getting these errors, what should I do?

/tmp/ccArqi4D.o: In function `boost::asio::detail::posix_event::posix_event()':
trial.cpp:(.text._ZN5boost4asio6detail11posix_eventC2Ev[_ZN5boost4asio6detail11posix_eventC5Ev]+0x4d): undefined reference to `pthread_condattr_setclock'
/tmp/ccArqi4D.o: In function `boost::asio::detail::posix_thread::~posix_thread()':
trial.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD5Ev]+0x26): undefined reference to `pthread_detach'
/tmp/ccArqi4D.o: In function `boost::asio::detail::posix_thread::join()':
trial.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2b): undefined reference to `pthread_join'
/tmp/ccArqi4D.o: In function `boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)':
trial.cpp:(.text._ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE[_ZN5boost4asio6detail12posix_thread12start_threadEPNS2_9func_baseE]+0x3a): undefined reference to `pthread_create'
/tmp/ccArqi4D.o: In function `boost::asio::detail::posix_signal_blocker::posix_signal_blocker()':
trial.cpp:(.text._ZN5boost4asio6detail20posix_signal_blockerC2Ev[_ZN5boost4asio6detail20posix_signal_blockerC5Ev]+0x64): undefined reference to `pthread_sigmask'
/tmp/ccArqi4D.o: In function `boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()':
trial.cpp:(.text._ZN5boost4asio6detail20posix_signal_blockerD2Ev[_ZN5boost4asio6detail20posix_signal_blockerD5Ev]+0x2d): undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status

1 Answers1

0

You're probably using an older version of boost. Considerably older: the docs to 1.66¹ already state:

https://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/io_service.html

enter image description here

Time Machine

Here's Live On Wandbox with 1.65.1:

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

int main() {
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
    t.wait();
    std::cout << "First app with asio!" << std::endl;
}

Prints

First app with asio!

Back To The Future

Again Live On Wandbox with 1.73.1:

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

int main() {
    boost::asio::io_context io;
    boost::asio::steady_timer t(io, boost::asio::chrono::seconds(5));
    t.wait();
    std::cout << "First app with asio!" << std::endl;
}

Prints

First app with asio!

¹ December 18th, 2017

sehe
  • 374,641
  • 47
  • 450
  • 633
  • I installed the latest version of boost. But I am getting errors(UPD in question) –  Jun 24 '20 at 11:01
  • Link a thread library, as the message probably says (it would normally say something something “DSO” followed by a library name. However for threads you can use the built-in compiler flag “-threads” which will do the right things – sehe Jun 24 '20 at 13:23
  • Wow. I put hte flag "-pthread" to the compiler and that work out. But why I have to do that, is it normal? Will it work properly every time? –  Jun 24 '20 at 14:51
  • @Egor Yeah it's not magic. There are no miracles in computing. [Just complexity](https://en.wikipedia.org/wiki/Clarke%27s_three_laws). For the general answer see [linking (phases of translation)](https://en.cppreference.com/w/cpp/language/translation_phases) and specifically [your error message](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). Note that [tag:c++-faq] tag :) – sehe Jun 24 '20 at 14:56
  • I heartily recommend understanding the compilation model, as these are the most frustrating kinds of problems to run into, and you always do. The sooner you get a handle on them, the better. – sehe Jun 24 '20 at 14:57
  • thank you so much! I am sure that i will ask for help again, so see you later:) –  Jun 24 '20 at 19:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216589/discussion-between-egor-and-sehe). –  Jun 24 '20 at 19:49