www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - noob in c macro preprocessor hell converting gsl library header files

reply data pulverizer <data.pulverizer gmail.com> writes:
I have been converting C numeric libraries and depositing them 
here: https://github.com/dataPulverizer. So far I have glpk and 
nlopt converted on a like for like c function basics. I am now 
stuck on the gsl library, primarily because of the preprocessor c 
code which I am very new to. The following few are particularly 
baffling to me:

#define INLINE_FUN extern inline // used in gsl_pow_int.h: 
INLINE_FUN double gsl_pow_2(const double x) { return x*x;   }

Could I just ignore the INLINE_FUN and use alias for function 
pointer declaration? For example ...

alias gsl_pow_2 = double gsl_pow_2(const(double) x);

#define INLINE_DECL // used in interpolation/gsl_interp.h: 
INLINE_DECL size_t gsl_interp_bsearch(const double x_array[], 
double x, size_t index_lo, size_t index_hi);

I would guess the same as for INLINE_FUN?

#define GSL_VAR extern // used in rng/gsl_rng.h:GSL_VAR const 
gsl_rng_type *gsl_rng_borosh13;

perhaps GSL_VAR can be ignored and I could use:

gsl_rng_borosh13 const(gsl_rng_type)*;

I have been using these kind of fixes and have not been able to 
get the rng module to recognise the ported functions, meaning 
that something has been lost in translation.

I am currently getting the following error:

$ gsl_rng_print_state

rng_example.o: In function `_Dmain':
rng_example.d:(.text._Dmain+0x13): undefined reference to 
`gsl_rng_print_state'
collect2: error: ld returned 1 exit status

I can't seem to call any of the functions but the types are 
recognized.

Thanks in advance
Jan 06
parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Wednesday, 6 January 2016 at 13:36:03 UTC, data pulverizer 
wrote:
 I have been converting C numeric libraries and depositing them 
 here: https://github.com/dataPulverizer. So far I have glpk and 
 nlopt converted on a like for like c function basics. I am now 
 stuck on the gsl library, primarily because of the preprocessor 
 c code which I am very new to. The following few are 
 particularly baffling to me:

 #define INLINE_FUN extern inline // used in gsl_pow_int.h: 
 INLINE_FUN double gsl_pow_2(const double x) { return x*x;   }

 Could I just ignore the INLINE_FUN and use alias for function 
 pointer declaration? For example ...

 alias gsl_pow_2 = double gsl_pow_2(const(double) x);
Yes, you should be able to ignore INLINE_FUN double gsl_pow_2(const double x); is the correct declaration.
 #define INLINE_DECL // used in interpolation/gsl_interp.h: 
 INLINE_DECL size_t gsl_interp_bsearch(const double x_array[], 
 double x, size_t index_lo, size_t index_hi);

 I would guess the same as for INLINE_FUN?
yes
 #define GSL_VAR extern // used in rng/gsl_rng.h:GSL_VAR const 
 gsl_rng_type *gsl_rng_borosh13;

 perhaps GSL_VAR can be ignored and I could use:

 gsl_rng_borosh13 const(gsl_rng_type)*;
It should be extern gsl_rng_type* gsl_rng_borosh13;
 I have been using these kind of fixes and have not been able to 
 get the rng module to recognise the ported functions, meaning 
 that something has been lost in translation.

 I am currently getting the following error:

 $ gsl_rng_print_state

 rng_example.o: In function `_Dmain':
 rng_example.d:(.text._Dmain+0x13): undefined reference to 
 `gsl_rng_print_state'
 collect2: error: ld returned 1 exit status

 I can't seem to call any of the functions but the types are 
 recognized.

 Thanks in advance
I think you might have some confusion between function declarations: T myFunction(Q myArg); function pointer type declarations: alias MyFunctionPointerType = T function(Q myArg); and function pointer declarations: MyFunctionPointerType myFunctionPointer;
Jan 06
parent data pulverizer <data.pulverizer gmail.com> writes:
On Wednesday, 6 January 2016 at 13:59:44 UTC, John Colvin wrote:
 #define INLINE_FUN extern inline // used in gsl_pow_int.h: 
 INLINE_FUN double gsl_pow_2(const double x) { return x*x;   }

 Could I just ignore the INLINE_FUN and use alias for function 
 pointer declaration? For example ...

 alias gsl_pow_2 = double gsl_pow_2(const(double) x);
Yes, you should be able to ignore INLINE_FUN double gsl_pow_2(const double x); is the correct declaration.
 #define GSL_VAR extern // used in rng/gsl_rng.h:GSL_VAR const 
 gsl_rng_type *gsl_rng_borosh13;

 perhaps GSL_VAR can be ignored and I could use:

 gsl_rng_borosh13 const(gsl_rng_type)*;
It should be extern gsl_rng_type* gsl_rng_borosh13;
I see. Thanks.
 I think you might have some confusion between function 
 declarations:

 T myFunction(Q myArg);

 function pointer type declarations:

 alias MyFunctionPointerType = T function(Q myArg);

 and function pointer declarations:

 MyFunctionPointerType myFunctionPointer;
Sorry in my haste to describe what I was doing I wrote down a function pointer instead of a function - my original code was a function. Your suggestion of looking at the https://github.com/abrown25/gsld library is a good call. I'll probably end up sending a pull request to that library after using it as a basic outline of how to deal with these preprocessors.
Jan 06