I would like to demonstrate a small sample of my project where I receive Linker error accessing a function defined with extern "C++". What would be the best way to access this function given that my project allows both C and C++ linkages?
/// Filename: new
#include <cstdlib>
#include <exception>
extern "C++" {
namespace std
{
typedef void (*new_handler)();
new_handler set_my_handler(new_handler) throw();
}
/// Filename: main.c
extern void handler();
void main()
{
handler();
}
/// Filename: App.cpp
#include <new>
static App objApp;
void no_memory()
{
// exit
}
void App::init()
{
using namespace std;
set_my_handler(no_memory);
}
extern "C" void handler()
{
objApp.init();
}
I get the following linker error:
../../obj/output/stm32-debug/Project/App.o: In function `App::init()': undefined reference to set_my_handler(void (*)())'
Earlier "set_my_handler" was defined to be extern "C" in the library, but now its extern "C++". I have not seen much usage of extern "C++" which restricts calls from C++ linkages alone. But in my project I have a C function that eventually accesses the C++ function described under extern "C++". Curious on why the linker cries as in above.
Observation: If I do not call objApp.init() [i.e. comment this line out] I do not get any linker error. The issue pertains with call flow from C towards C++ function marked extern "C++".