anio
anio

Reputation: 9161

passing function pointer not working

I have 2 libraries. Library A is compiled in C. Library B is compiled in C++, but it is mostly C code.

I need to call the following function in library A:

foo* c_func(int64_t (*ptr_to_func)(void));

The first thing this function does is:

if(!ptr_to_func)
  return NULL;

The ptr_to_func needs to point to a function in library B which is declared as:

int64_t bar(void);

I have a cpp file with a main function that calls another function, in there I do the following:

foo* f = c_func(bar);

Then I check if f is null. It is.

What am I doing wrong?

Upvotes: 0

Views: 194

Answers (1)

caf
caf

Reputation: 239121

What you are doing appears to be correct, with the caveat that your function bar() should be declared within an extern "C" { section.

c_func() is probably returning NULL for a different reason. Step through it in the debugger and see (set a breakpoint at the beginning of c_func()).

Upvotes: 4

Related Questions