Question on using C++ API
Thorsten Lück
T.Lueck at IFEN.com
Wed Nov 18 17:38:15 CET 2015
Hi @all,
I want to write a small embedded application listing on UDP. It is
compiling using the C-Api but not using the C++-Api. In both cases I am
using the gnu g++ compiler:
> $ /opt/crosstool-ng-powerpc/bin/powerpc-e500v2-linux-gnuspe-g++ --version
> powerpc-e500v2-linux-gnuspe-g++ (crosstool-NG 1.16.0-WORK-1.0) 4.6.0
> Copyright (C) 2011 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
<snip>
// This callback is called when data is readable on the UDP socket.
static void
udp_cb (EV_P_ ev_io *w, int revents)
{
puts("udp socket has become readable");
socklen_t bytes = recvfrom(sd, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr*) &addr, (socklen_t *) &addr_len);
// add a null to terminate the input, as we're going to use it as a
string
buffer[bytes] = '\0';
printf("udp client said: %s", buffer);
// Echo it back.
// WARNING: this is probably not the right way to do it with libev.
// Question: should we be setting a callback on sd becomming
writable here instead?
sendto(sd, buffer, bytes, 0, (struct sockaddr*) &addr, sizeof(addr));
}
struct udp_client
{
void operator() (EV_P_ ev_io &w, int revents)
{
puts("udp socket has become readable");
socklen_t bytes = recvfrom(sd, buffer, sizeof(buffer) - 1, 0,
(struct sockaddr*) &addr, (socklen_t *) &addr_len);
// add a null to terminate the input, as we're going to use it
as a string
buffer[bytes] = '\0';
printf("udp client said: %s", buffer);
// Echo it back.
// WARNING: this is probably not the right way to do it with libev.
// Question: should we be setting a callback on sd becomming
writable here instead?
sendto(sd, buffer, bytes, 0, (struct sockaddr*) &addr,
sizeof(addr));
}
};
<snip>
#define COMP_EV_CPP
#ifdef COMP_EV_CPP
udp_client myCb;
ev::io udp_watcher;
udp_watcher.set( &myCb );
udp_watcher.start();
#else
ev_io udp_watcher;
ev_io_init(&udp_watcher, udp_cb, sd, EV_READ);
ev_io_start(EV_A_ &udp_watcher);
#endif
ev_loop(loop, 0);
<snip>
Using the C++-API (#define COMP_EV_CPP) creates the following error:
> [ 50%] Building CXX object CMakeFiles/SG-App.dir/main.cpp.o
> /opt/crosstool-ng-powerpc/bin/powerpc-e500v2-linux-gnuspe-g++ -I/home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/. -I/home/thl/Projekte/Tools -I/home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/../libev -I/home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/build -o CMakeFiles/SG-App.dir/main.cpp.o -c /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/main.cpp
> In file included from /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/SG_ev.h:6:0,
> from /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/main.cpp:31:
> /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/../libev/ev++.h: In member function 'void ev::base<ev_watcher, watcher>::set(K*) [with K = udp_client, ev_watcher = ev_io, watcher = ev::io]':
> /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/main.cpp:159:28: instantiated from here
> /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/../libev/ev++.h:473:7: error: no matching function for call to 'ev::base<ev_io, ev::io>::set_(udp_client*&, <unresolved overloaded function type>)'
> /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/../libev/ev++.h:473:7: note: candidate is:
> /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/../libev/ev++.h:442:10: note: void ev::base<ev_watcher, watcher>::set_(const void*, void (*)(ev_loop*, ev_watcher*, int)) [with ev_watcher = ev_io, watcher = ev::io]
> /home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/../libev/ev++.h:442:10: note: no known conversion for argument 2 from '<unresolved overloaded function type>' to 'void (*)(ev_loop*, ev_io*, int)'
> make[2]: *** [CMakeFiles/SG-App.dir/main.cpp.o] Error 1
> make[2]: Leaving directory `/home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/build'
> make[1]: *** [CMakeFiles/SG-App.dir/all] Error 2
> make[1]: Leaving directory `/home/thl/Projekte/NavX-NCS/branches/trunk_Qt5/SG/SGApp/build'
> make: *** [all] Error 2
I get the same error when compiling "natively" on Ubuntu 14.04 using
> $ /usr/bin/c++ --version
> c++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
> Copyright (C) 2013 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions. There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Does anybody has an idea what I am doing wrong?
Thanks for any help,
Thorsten
--
------------------------------------------------------------------------
Dr.-Ing. Thorsten Lueck
Head of Navigation Products
Product Manager NavX-NCS
IFEN GmbH
Alte Gruber Strasse 6
D-85586 Poing
Tel: +49-8121-2238-25
Fax: +49-8121-2238-11
Web: www.ifen.com
------------------------------------------------------------------------
More information about the libev
mailing list