[Boost.Asio] tutorial #1 code does not compile (g++)

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

[Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
Hi All,

I am attempting to get acquainted with C++ multithreaded network programming. I have a background in such topics within Java but considering how different C++ is, I am starting from ground zero.

I have copied the code from tutorial #1 into my IDE, Timer.1: Using a timer synchronously: https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html (follow the link for code).

When I tried to compile this (using g++) with the following command:

g++ -v -I${BOOST_ROOT} -I. -o main main.cpp

(BOOST_ROOT is where I have boost installed), I get the following output:

Undefined symbols for architecture x86_64:

  "boost::chrono::steady_clock::now()", referenced from:

      boost::asio::detail::chrono_time_traits<boost::chrono::steady_clock, boost::asio::wait_traits<boost::chrono::steady_clock> >::now() in main-c86634.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Note this is the non-verbose output (I would rather not post the verbose output, as I am trying to keep this post short).

I did some digging of my own on Boost's steady_timer documentation and tried to follow that style, as you can find here:
In other words, I replace the two-parameter boost::asio::steady_timer ctor call with a single-parameter ctor call in which I pass an io_context, followed by 
t.expires_after(std::chrono::seconds(5));

This generated another compiler error, this one to do with 'no viable conversion'. See here:

main.cpp:10:21: error: no viable conversion from 'std::chrono::seconds' (aka

      'duration<long long>') to 'const

      boost::asio::basic_waitable_timer<boost::chrono::steady_clock,

      boost::asio::wait_traits<boost::chrono::steady_clock>,

      boost::asio::executor>::duration' (aka 'const duration<long long,

      ratio<(1L), (1000000000L)> >')

    t.expires_after(std::chrono::seconds(5));

Again, there is more output that I have omitted for brevity. If anyone would like to see the rest of the output, I will gladly supply it.

The bottom line is I am not sure what the problem is, or why I am having to debug tutorial code in the first place. This should be relatively simple to "plug & play", should it not? Does this have to do with my compiler? BTW, I am running Mac OS X.

Thanks in advance for the help.

Sincerely,
AJ

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
On Tue, Jan 28, 2020 at 5:41 PM Andrew McFarlane via Boost-users
<[hidden email]> wrote:
> I have copied the code from tutorial #1 into my IDE,
> Timer.1: Using a timer synchronously: > https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html

You're using the example code from Boost 1.72.0. Are you also using
the asio headers from Boost 1.72.0? Or do you have an older version of
Boost installed (say, the one that comes with your OS distribution)?

Thanks
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list

Your instinct was right, I was using Boost 1.71.0, though replacing that version with 1.72.0 has not fixed the issue. I am getting the same error message as before in both cases.

-AJ


On Tue, Jan 28, 2020 at 6:04 PM Vinnie Falco <[hidden email]> wrote:
On Tue, Jan 28, 2020 at 5:41 PM Andrew McFarlane via Boost-users
<[hidden email]> wrote:
> I have copied the code from tutorial #1 into my IDE,
> Timer.1: Using a timer synchronously: > https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html

You're using the example code from Boost 1.72.0. Are you also using
the asio headers from Boost 1.72.0? Or do you have an older version of
Boost installed (say, the one that comes with your OS distribution)?

Thanks

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
Don’t you need to add -lboost_chrono to the gcc command line?

Gcc does not do automatic linker comment insertion.

On Wed, 29 Jan 2020 at 08:26, Andrew McFarlane via Boost-users <[hidden email]> wrote:

Your instinct was right, I was using Boost 1.71.0, though replacing that version with 1.72.0 has not fixed the issue. I am getting the same error message as before in both cases.

-AJ


On Tue, Jan 28, 2020 at 6:04 PM Vinnie Falco <[hidden email]> wrote:
On Tue, Jan 28, 2020 at 5:41 PM Andrew McFarlane via Boost-users
<[hidden email]> wrote:
> I have copied the code from tutorial #1 into my IDE,
> Timer.1: Using a timer synchronously: > https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html

You're using the example code from Boost 1.72.0. Are you also using
the asio headers from Boost 1.72.0? Or do you have an older version of
Boost installed (say, the one that comes with your OS distribution)?

Thanks
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
--
Richard Hodges
office: +442032898513
home: +376841522
mobile: +376380212


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
Le 29/01/2020 à 02:14, Andrew McFarlane via Boost-users a écrit :
> Undefined symbols for architecture x86_64:
>
> "boost::chrono::steady_clock::now()", referenced from:
>
> boost::asio::detail::chrono_time_traits<boost::chrono::steady_clock,
> boost::asio::wait_traits<boost::chrono::steady_clock> >::now() in
> main-c86634.o

I think you'll need to link against boost_chrono then.

--
David


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
Hi Richard,

I suspect you and David are both correct; I need to link boost_chrono. I found here

"If BOOST_CHRONO_HEADER_ONLY is not defined you need to compile it and build the library before use". This aligns with what you are saying. 

However, the command it mentions to use,

>> bjam libs/chrono/build

does not work, even when I am in the boost directory (bjam is not defined).

In addition, adding -lboost_chrono or -L{BOOST_ROOT} does not help my case. After reading this article about static and dynamic linked libraries
I suspect I must append the path of boost_chrono to my LD_LIBRARY_PATH variable. Am I correct in this thinking? If so, where the heck is boost_chrono located? I could not find a .a or .so file in libs/chrono/ anywhere. Do I need to build the static/dynamic library myself? 
Please advise. 

I apologize for the elementary questions; my compiler and Unix knowledge could be way better, which is something I am working on.

Thanks

Andrew J. E. McFarlane
Cell: (236) 888-1376


On Tue, Jan 28, 2020 at 11:33 PM <[hidden email]> wrote:
Send Boost-users mailing list submissions to
        [hidden email]

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.boost.org/mailman/listinfo.cgi/boost-users
or, via email, send a message with subject or body 'help' to
        [hidden email]

You can reach the person managing the list at
        [hidden email]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Boost-users digest..."


Today's Topics:

   1. [Boost.Asio] tutorial #1 code does not compile (g++)
      (Andrew McFarlane)
   2. Re: [Boost.Asio] tutorial #1 code does not compile (g++)
      (Vinnie Falco)
   3. Re: [Boost.Asio] tutorial #1 code does not compile (g++)
      (Andrew McFarlane)
   4. Re: [Boost.Asio] tutorial #1 code does not compile (g++)
      (Richard Hodges)


----------------------------------------------------------------------

Message: 1
Date: Tue, 28 Jan 2020 17:14:58 -0800
From: Andrew McFarlane <[hidden email]>
To: [hidden email]
Subject: [Boost-users] [Boost.Asio] tutorial #1 code does not compile
        (g++)
Message-ID:
        <CAG90r9SPGR=[hidden email]>
Content-Type: text/plain; charset="utf-8"

Hi All,

I am attempting to get acquainted with C++ multithreaded network
programming. I have a background in such topics within Java but considering
how different C++ is, I am starting from ground zero.

I have copied the code from tutorial #1 into my IDE, Timer.1: Using a timer
synchronously:
https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html
(follow
the link for code).

When I tried to compile this (using g++) with the following command:

g++ -v -I${BOOST_ROOT} -I. -o main main.cpp
(BOOST_ROOT is where I have boost installed), I get the following output:

Undefined symbols for architecture x86_64:

  "boost::chrono::steady_clock::now()", referenced from:

      boost::asio::detail::chrono_time_traits<boost::chrono::steady_clock,
boost::asio::wait_traits<boost::chrono::steady_clock> >::now() in
main-c86634.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see
invocation)
Note this is the non-verbose output (I would rather not post the verbose
output, as I am trying to keep this post short).

I did some digging of my own on Boost's steady_timer documentation and
tried to follow that style, as you can find here:
https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/reference/steady_timer.html
In other words, I replace the two-parameter boost::asio::steady_timer ctor
call with a single-parameter ctor call in which I pass an io_context,
followed by
t.expires_after(std::chrono::seconds(5));

This generated another compiler error, this one to do with 'no viable
conversion'. See here:

main.cpp:10:21: error: no viable conversion from 'std::chrono::seconds' (aka

      'duration<long long>') to 'const

      boost::asio::basic_waitable_timer<boost::chrono::steady_clock,

      boost::asio::wait_traits<boost::chrono::steady_clock>,

      boost::asio::executor>::duration' (aka 'const duration<long long,

      ratio<(1L), (1000000000L)> >')

    t.expires_after(std::chrono::seconds(5));
Again, there is more output that I have omitted for brevity. If anyone
would like to see the rest of the output, I will gladly supply it.

The bottom line is I am not sure what the problem is, or why I am having to
debug tutorial code in the first place. This should be relatively simple to
"plug & play", should it not? Does this have to do with my compiler? BTW, I
am running Mac OS X.

Thanks in advance for the help.

Sincerely,

*AJ*
-------------- next part --------------
HTML attachment scrubbed and removed

------------------------------

Message: 2
Date: Tue, 28 Jan 2020 18:04:31 -0800
From: Vinnie Falco <[hidden email]>
To: Boost users list <[hidden email]>
Cc: Andrew McFarlane <[hidden email]>
Subject: Re: [Boost-users] [Boost.Asio] tutorial #1 code does not
        compile (g++)
Message-ID:
        <CA+EzHGdAnjYQZgqqhKuz2Rd7p9th+0PLKtB_puc5=[hidden email]>
Content-Type: text/plain; charset="UTF-8"

On Tue, Jan 28, 2020 at 5:41 PM Andrew McFarlane via Boost-users
<[hidden email]> wrote:
> I have copied the code from tutorial #1 into my IDE,
> Timer.1: Using a timer synchronously: > https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html

You're using the example code from Boost 1.72.0. Are you also using
the asio headers from Boost 1.72.0? Or do you have an older version of
Boost installed (say, the one that comes with your OS distribution)?

Thanks


------------------------------

Message: 3
Date: Tue, 28 Jan 2020 23:26:12 -0800
From: Andrew McFarlane <[hidden email]>
To: Vinnie Falco <[hidden email]>
Cc: Boost users list <[hidden email]>
Subject: Re: [Boost-users] [Boost.Asio] tutorial #1 code does not
        compile (g++)
Message-ID:
        <[hidden email]>
Content-Type: text/plain; charset="utf-8"

Your instinct was right, I was using Boost 1.71.0, though replacing that
version with 1.72.0 has not fixed the issue. I am getting the same error
message as before in both cases.

-AJ


On Tue, Jan 28, 2020 at 6:04 PM Vinnie Falco <[hidden email]> wrote:

> On Tue, Jan 28, 2020 at 5:41 PM Andrew McFarlane via Boost-users
> <[hidden email]> wrote:
> > I have copied the code from tutorial #1 into my IDE,
> > Timer.1: Using a timer synchronously: >
> https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html
>
> You're using the example code from Boost 1.72.0. Are you also using
> the asio headers from Boost 1.72.0? Or do you have an older version of
> Boost installed (say, the one that comes with your OS distribution)?
>
> Thanks
>
-------------- next part --------------
HTML attachment scrubbed and removed

------------------------------

Message: 4
Date: Wed, 29 Jan 2020 08:29:38 +0100
From: Richard Hodges <[hidden email]>
To: [hidden email]
Subject: Re: [Boost-users] [Boost.Asio] tutorial #1 code does not
        compile (g++)
Message-ID:
        <[hidden email]>
Content-Type: text/plain; charset="utf-8"

Don?t you need to add -lboost_chrono to the gcc command line?

Gcc does not do automatic linker comment insertion.

On Wed, 29 Jan 2020 at 08:26, Andrew McFarlane via Boost-users <
[hidden email]> wrote:

>
> Your instinct was right, I was using Boost 1.71.0, though replacing that
> version with 1.72.0 has not fixed the issue. I am getting the same error
> message as before in both cases.
>
> -AJ
>
>
> On Tue, Jan 28, 2020 at 6:04 PM Vinnie Falco <[hidden email]>
> wrote:
>
>> On Tue, Jan 28, 2020 at 5:41 PM Andrew McFarlane via Boost-users
>> <[hidden email]> wrote:
>> > I have copied the code from tutorial #1 into my IDE,
>> > Timer.1: Using a timer synchronously: >
>> https://www.boost.org/doc/libs/1_72_0/doc/html/boost_asio/tutorial/tuttimer1.html
>>
>> You're using the example code from Boost 1.72.0. Are you also using
>> the asio headers from Boost 1.72.0? Or do you have an older version of
>> Boost installed (say, the one that comes with your OS distribution)?
>>
>> Thanks
>>
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
>
--
Richard Hodges
[hidden email]
office: +442032898513
home: +376841522
mobile: +376380212
-------------- next part --------------
HTML attachment scrubbed and removed

------------------------------

Subject: Digest Footer

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


------------------------------

End of Boost-users Digest, Vol 5314, Issue 1
********************************************

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
On 30/01/2020 07:49, Andrew McFarlane wrote:
> I suspect you and David are both correct; I need to link boost_chrono.
[...]
> In addition, adding -lboost_chrono or -L{BOOST_ROOT} does not help my
> case. After reading this article about static and dynamic linked libraries
> https://medium.com/@dkwok94/the-linking-process-exposed-static-vs-dynamic-libraries-977e92139b5f
> I suspect I must append the path of boost_chrono to my LD_LIBRARY_PATH
> variable. Am I correct in this thinking? If so, where the heck is
> boost_chrono located? I could not find a .a or .so file in libs/chrono/
> anywhere. Do I need to build the static/dynamic library myself?
> Please advise.

https://www.boost.org/doc/libs/1_72_0/more/getting_started/unix-variants.html


Note that unless you need something from the latest Boost, you can also
install the corresponding dev packages (via your standard package
manager) to use your system's Boost version instead, just like any other
library packaged by your distro.  This will save you some time.
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
Okay, so I have made a bit of progress. After building Boost.Asio with:
b2 --with-system --with-thread --with-date_time --with-regex --with-serialization stage
, and building Boost.Chrono with (replacing bjam with b2):
bjam libs/chrono/build
The code is now compiling with the following g++ command:

g++ -v -Wall -L ${BOOST_ROOT}/stage/lib -L ${BOOST_ROOT}/bin.v2/libs/chrono/build/clang-darwin-11.0/debug/threading-multi/visibility-hidden -lboost_chrono -lboost_date_time -lboost_system -I. -I${BOOST_ROOT} -o main main.cpp


However, when I go to run main, the program crashes with the following message: 

dyld: Library not loaded: @rpath/libboost_chrono.dylib

  Referenced from: /Users/ajm/Projects/boost-threads/ex1/./main

  Reason: image not found

Abort trap: 6


I suspect that there is something wrong with the Boost.Chrono dynamic library file that was built (probably because of how I built it), but I am not sure what it would be. Any suggestions?

Thanks,

Andrew J. E. McFarlane

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
On 1/30/2020 5:35 PM, Andrew McFarlane via Boost-users wrote:

> Okay, so I have made a bit of progress. After building Boost.Asio with:
>
> b2 --with-system --with-thread --with-date_time --with-regex --with-serialization stage
>
> , and building Boost.Chrono with (replacing bjam with b2):
>
> bjam  libs/chrono/build
>
> The code is now compiling with the following g++ command:
>
> g++ -v -Wall -L ${BOOST_ROOT}/stage/lib -L
> ${BOOST_ROOT}/bin.v2/libs/chrono/build/clang-darwin-11.0/debug/threading-multi/visibility-hidden
> -lboost_chrono -lboost_date_time -lboost_system -I. -I${BOOST_ROOT} -o
> main main.cpp
>
> /
> /
> However, when I go to run main, the program crashes with the following
> message:
>
> dyld: Library not loaded: @rpath/libboost_chrono.dylib
>
> Referenced from: /Users/ajm/Projects/boost-threads/ex1/./main
>
> Reason: image not found
>
> Abort trap: 6
>
> /
> /
> I suspect that there is something wrong with the Boost.Chrono dynamic
> library file that was built (probably because of how I built it), but I
> am not sure what it would be. Any suggestions?

Lookup RPATH for Linux.

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: [Boost.Asio] tutorial #1 code does not compile (g++)

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
On 31/01/2020 11:35, Andrew McFarlane wrote:
> Okay, so I have made a bit of progress. After building Boost.Asio with:
>
> b2 --with-system --with-thread --with-date_time --with-regex --with-serialization stage
>
> , and building Boost.Chrono with (replacing bjam with b2):
>
> bjam  libs/chrono/build

You could have just added --with-chrono to the first command.

> The code is now compiling with the following g++ command:
>
> g++ -v -Wall -L ${BOOST_ROOT}/stage/lib -L
> ${BOOST_ROOT}/bin.v2/libs/chrono/build/clang-darwin-11.0/debug/threading-multi/visibility-hidden
> -lboost_chrono -lboost_date_time -lboost_system -I. -I${BOOST_ROOT} -o
> main main.cpp

You should not link with libs directly in bin.v2; use the stage/lib
directory instead.

(They're not different, it's just that the bin.v2 path is fragile.)

> However, when I go to run main, the program crashes with the following message:
>
> dyld: Library not loaded: @rpath/libboost_chrono.dylib
>
>   Referenced from: /Users/ajm/Projects/boost-threads/ex1/./main
>
>   Reason: image not found
>
> Abort trap: 6

You either need to tell your compiler to use the static libraries (it
usually defaults to using dynamic libraries), or you need to set the
RPATH at compile time to the (absolute path of the) stage/lib folder, or
the LD_LIBRARY_PATH when you run the app (less preferred).

Or use the system-provided libraries instead, which will Just Work™
without any of this.
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users