I am currently trying to enable A.cpp from A folder to use a function func() defined in B.cpp from B folder.
The top level Makefile shows: DEPENDS = ... B A ....
Although I know very little about Makefiles, my boss told me the above Makefile statement indicates some hierarchy and dependency of the entire project. So I could not directly call func() defined in B.cpp from A.cpp. And he said I was silly to try to copy the entire dependency from B folder to A folder earlier. At last, he suggested me to use "Call back function" in this situation.
Now my basic understand of "Call back function" is that I need to pass "func" as an argument (pointer to func()) to some functions in A.cpp. However, I don't quite understand the motivation of using "callback function" in my case and how to realize it.
Could anybody give me some instructions? Thanks :-)
So I did the following things:
/* Inside /A/A.h */
typedef void(*PTR_FUNC_B)();
void SetCB(PTR_FUNC_B fn);
/* Inside /A/A.cpp */
PTR_FUNC_B myFn;
void SetCB(PTR_FUNC_B fn) {myFn = fn}
some_func_of_A () {
myFn() // segmentation fault
}
/* Inside /B/B.cpp */
void func_b() {
std::printf("Invoke callback function.");
}
void register_cb() {
SetCB(func_b);
}
Is my understanding of callback functions correct? I am glad I can compile it successfully, but I got "segmentation fault accessing address 0".