简体   繁体   中英

VRPN C++ code compiles on Linux but not Windows

I've built a VRPN client on Linux. It's based on this: http://www.vrgeeks.org/vrpn/tutorial---use-vrpn

Here's some of the code:

vrpn_Analog_Remote * analog = NULL;
vrpn_Button_Remote * button = NULL;
vrpn_Tracker_Remote * tracker = NULL;

// Things happen...

analog = new vrpn_Analog_Remote("pathToAnalog");
analog->register_change_handler(NULL, handleAnalog);
button = new vrpn_Button_Remote("pathToButton");
button->register_change_handler(NULL, handleButton);
tracker = new vrpn_Tracker_Remote("pathToTracker");
tracker->register_change_handler(NULL, handleTracker);

Here are the callbacks refered to in this code:

void handleAnalog(void * userData, const vrpn_ANALOGCB a) {
  // Do stuff...
}
void handleButton(void * userData, const vrpn_BUTTONCB b) {
  // Do stuff...
}
void handleTracker(void * userData, const vrpn_TRACKERCB t) {
  // Do stuff...
}

And here is where all of these references to VRPN are defined:

https://github.com/vrpn/vrpn/blob/master/vrpn_Analog.h#L168 https://github.com/vrpn/vrpn/blob/master/vrpn_Button.h#L225 https://github.com/vrpn/vrpn/blob/master/vrpn_Tracker.h#L284

These compile without even a warning on Linux and can actually be used. Everything worked as expected. All the types here seem to satisfy the compiler, g++.

But on Windows, whether I use Visual Studio 2015 or MinGW's g++, I get this for the first two callback registrations:

invalid conversion from 'void (*)(void*, vrpn_ANALOGCB) {aka void (*)(void*, _vrpn_ANALOGCB)}' to 'vrpn_ANALOGCHANGEHANDLER {aka 
 void (__attribute__((__stdcall__)) *)(void*, _vrpn_ANALOGCB)}' [-fpermissive]

invalid conversion from 'void (*)(void*, vrpn_BUTTONCB) {aka void (*)(void*, _vrpn_BUTTONCB)}' to 'vrpn_BUTTONCHANGEHANDLER {aka 
 void (__attribute__((__stdcall__)) *)(void*, _vrpn_BUTTONCB)}' [-fpermissive]

And for the last one, I get a different error:

call of overloaded 'register_change_handler(NULL, void (&)(void*, vrpn_TRACKERCB))' is 
 ambiguous

Now that I'm typing this, I'm thinking maybe VRPN was compiled differently on Windows and that's why the compiler now has a problem with my code. But I'm very lost as to what to do.

Try declaring your callbacks like this:

void VRPN_CALLBACK handleAnalog(void * userData, const vrpn_ANALOGCB a) {
  // Do stuff...
}

For linux, the VRPN_CALLBACK define is empty, so you did not notice any problems there. For windows, the VRPN library devs decided that they expect a callback that adheres to the __stdcall calling-convention. Thus you have to declare your function accordingly, and the most painless+portable way is to use the very same VRPN_CALLBACK define that they provide.

Clues to this came from your links to the code at github:

[vrpn_Analog.h]
typedef void(VRPN_CALLBACK *vrpn_ANALOGCHANGEHANDLER)(void *userdata,
                                                      const vrpn_ANALOGCB info);

and the callback define is made here:

[vrpn_Configure.h]
#ifdef _WIN32   // [ ...
#define VRPN_CALLBACK __stdcall
#else // ... ] WIN32 [
#define VRPN_CALLBACK
#endif // ] not WIN32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM