How do handle multiple versions of a method in my node
addon
when compiling across multiple versions of node. For ex: uv_inet_pton
and uv_inet_pton
of node
version 0.10*
is different than 0.12.*
. The return type is different in these versions. Can I handle this case by defining a Macro or a pre processor ?
Asked
Active
Viewed 389 times
0

Royal Pinto
- 2,869
- 8
- 27
- 39
-
1So you want a macro that does something on version `0.10*` and something else in `0.12*`, am I correct? Is there a way to fetch the version in compile time? If so, you can define version-specific macros. Or better, have a function that wraps one of them in one version, and directly calls in the other. – holgac Apr 13 '15 at 13:12
-
@holgac yeah, its possible to check the version at compile time. But I am wondering if its possible to check the method signature(return type) and define Macro accordingly(sorry I am new to `c`/`ccp`.). If its not possible then will go with the first option that you have suggested. For the second option, if I define a wrap function, wouldn't it conflict with the existing function in one version of `node`...? – Royal Pinto Apr 13 '15 at 15:27
-
1AFAIK there is no way to do that. For your second question, yes it would conflict, so you need to give your wrapper a different name. – holgac Apr 13 '15 at 15:46
-
@holgac Thanks for your reply and related link. For second option, if I have to write a wrap function(with different name), then inside wrap function, i would have to check for the version using preprocessor. Isn't it similar to the first option or my understanding of second option is wrong ? – Royal Pinto Apr 13 '15 at 15:59
-
No, because in the first option you're trying to detect what a function returns. In the second option, you know what it returns in which version, so you'll need to write version-specific code. See my answer below for an example. – holgac Apr 13 '15 at 16:30
1 Answers
1
The signature in 0.10 is:
typedef struct uv_err_s uv_err_t;
uv_err_t uv_inet_pton(int af, const char* src, void* dst);
struct uv_err_s {
/* read-only */
/* MY ADDITION:
uv_err_code is an enum here, and 0 means OK.
*/
uv_err_code code;
/* private */
int sys_errno_;
};
So a call to this function returns a structure which has a code
field, which specifies the error, which is 0 for success.
As for newer versions, the return type has changed:
UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);
So, using version information provided in newer and older versions, you can deduce which version you're compiling against. The links are for 0.10
and 1.0x
, but you can get similar information in 0.12
as well.
If it's OK for a function that returns int, 0 for success, other values for failure, you need to use the function provided below:
int my_inet_pton(int af, const char* src, void* dst)
{
#ifdef VERSION_0_10_CHECK
/* I'm a little rusty on enums, you might need to
* cast to int, i'm not sure */
return uv_inet_pton(af, src, dst).code;
#else
return uv_inet_pton(af, src, dst);
#endif
}
For other possible incompatibilities, try using version checking as little as possible in the main code, and write wrappers for the dirty work. This way, your main code will look nicer.

holgac
- 1,509
- 1
- 13
- 25