bug-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Bug-gsl] using gsl callback routine from c++ / pointer to member fu


From: Jonathan Taylor
Subject: Re: [Bug-gsl] using gsl callback routine from c++ / pointer to member function
Date: Wed, 13 Feb 2013 17:56:53 +0000

> I'm trying to use GSL's multimin routine from C++, but the same issue
> arises with integration and any functionality that uses a callback function
> to calculate a user function f(x). The calls to GSL and to the callback
> function are in a class. I am using g++ 4.1.2 and GSL 1.13.
> 
> l define a function myF() to compute f(x), and pass GSL a pointer to myF().
> The problem is that the compiler won't convert from "pointer to myF ()" to
> the way that GSL declares the pointer. This is because C++ treats a
> "pointer to member function" differently from how it treats a pointer to a
> function that is not part of a class. I understand the C++ issue; I hope
> that somebody on this list knows how to get around it.

I'm afraid this is a completely standard issue, and I am pretty sure there is 
no particularly elegant solution to it. Your part 1 and part 2 will not and can 
not work, that's just basic violations of how the language requires you to 
behave, I'm afraid. The function types really are different. [As you may well 
know, a C++ function can be thought of as akin to a C function which takes an 
extra, first, parameter: the pointer to "this"]. No amount of typecasting can 
hide that.

Part 3 should work. The standard format is to do something like this (written 
in email, not compiled):

class MyClass
{
        double MyCallback(const gsl_vector *x);
        static double MyStaticCallback(const gsl_vector *x, void *params) { 
return ((MyClass*)params)->MyCallback(x); }
};

i.e. you declare a second static helper function for any class-based callback 
you require.


> Another suggestion was to make myF() static, ugly though that may be. I
> tried that, and got an error message that the minimization routine could
> not be found. Oddly, this was a runtime error. The linker did not complain,
> so presumably it found the nmsimplex2 routine, but somehow the routine
> could not be found at runtime.
> 
>    In the code: const gsl_multimin_fminimizer_type *T =
> gsl_multimin_fminimizer_nmsimplex2
> 
>    Runtime error: Unrecognized symbol gsl_multimin_fminimizer_nmsimplex2

I'm 99% sure this has nothing to do with your question about C++ callbacks. I 
can only guess that you are weakly linking to the GSL library and the weak 
linkage is failing at runtime, or something like that.

Hope this helps
Jonny


reply via email to

[Prev in Thread] Current Thread [Next in Thread]