开发者

passing function pointer not working

开发者 https://www.devze.com 2023-02-14 13:22 出处:网络
I have 2 libraries. Library A is compiled in C. Library B is compiled in C++, but it is mostly C code.

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 func开发者_StackOverflow中文版tion 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?


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()).

0

精彩评论

暂无评论...
验证码 换一张
取 消