Re: Wrapping Callback Functions

new topic     » goto parent     » topic index » view thread      » older message » newer message
Icy_Viking said...
petelomax said...
Icy_Viking said...

Is the name in quotes not supposed to be there?

YOU CANNOT BE SERIOUS!!! The names in quotes are supposed to be spelt correctly....

(And yes/as irv said, a + may well help)

Oops, just noticed the typo. My eyesight isn't what it used to be. I thought something was wrong with the entire quotes.

So this raises the following questions:

What is the '+' for?

The '+' character is used to force the cdecl calling convention on 32-bit Windows, where the default is stdcall (which is strictly an x86 convention). On any other system, and all 64-bit systems, the default is already cdecl, so the '+' has no effect.

Therefore, if you're using a library that requires cdecl on 32-bit Windows, you can wrap it with '+' in the function names without having to maintain two separate versions with and without '+' everywhere.

How do we know when to use '+'?

This can be a little tricky. You need to dig into the code a little. If you walk through the header you should see if it defines any particular calling convention.

If we start at the definition for a function, like NewtonSetMemorySystem we see it's defined as:

NEWTON_API void NewtonSetMemorySystem (NewtonAllocMemory malloc, NewtonFreeMemory free); 

That NEWTON_API macro is likely going to contain our calling convention, if the authors of the library have intended to change it.

Sure enough, if we scroll up to the top of Newton.h we find several ifdef's that control the content of the NEWTON_API macro.

#if defined(_MSC_VER) 
    #define DG_LIBRARY_EXPORT __declspec(dllexport) 
    #define DG_LIBRARY_IMPORT __declspec(dllimport) 
    #define DG_LIBRARY_STATIC 
#else 
    #define DG_LIBRARY_EXPORT __attribute__((visibility("default"))) 
    #define DG_LIBRARY_IMPORT __attribute__((visibility("default"))) 
    #define DG_LIBRARY_STATIC 
#endif 
 
#ifdef _NEWTON_STATIC_LIB 
    #define NEWTON_API DG_LIBRARY_STATIC 
#elif defined(_NEWTON_BUILD_DLL) 
    #define NEWTON_API DG_LIBRARY_EXPORT 
#else 
    #define NEWTON_API DG_LIBRARY_IMPORT 
#endif 

Which means if the library is built as shared library (dll/so) and on Visual Studio (where _MSC_VER is defined) then the value of NEWTON_API will become __declspec(dllexport), which is the cdecl calling convention.

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu