0

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++".

Sammy
  • 257
  • 2
  • 8
  • I think the person downvoting did not go through my question before downvoting. This is not a duplicate question. It is a question pertaining to extern "C++" function access and not the same question. Request you to kindly revert the downvote. – Sammy Jul 19 '18 at 20:56
  • There is a clear demarcation between extern "C" and extern "C++". The latter is not a common usage and the question highlights the latter. I believe one would agree. – Sammy Jul 19 '18 at 21:02
  • I don't think this is a duplicate question since there is a difference between extern C and extern C++. – hago Jul 19 '18 at 21:05
  • @hago I am pretty sure that the primary dupe covers that. – πάντα ῥεῖ Jul 19 '18 at 21:11
  • Correction: Removed the duplicate extern. My bad, was a typo. The linker error is still relevant. – Sammy Jul 19 '18 at 21:12
  • @πάντα ῥεῖ , I read the primary dupe. Eventhough it is tagged for C++, there is no example for extern "C++" which is the question by this OP. So, I believe that, this question is relevant and not a duplicate. – hago Jul 19 '18 at 21:18
  • @hago It's still about symbol mangling mismatch anyhow. No matter of direction coming from `extern "C++"` or `extern "C"`. – πάντα ῥεῖ Jul 19 '18 at 21:29
  • 1
    Even if the question is reoppened, it'll probably be closed again for lack of a [mcve]. – user4581301 Jul 19 '18 at 21:39
  • 1
    You put the function into namespace `std` (which is UB but it probably doesn't cause the linker error.) So it needs to be referenced as `std::set_my_handler`. But don't do that. Put it into your own namespace. (And what do you think `extern "C++"` buys you?) – rici Jul 19 '18 at 21:54
  • Thanks @rici appreciate someone at least addressed an attempt to answer the question than unnecessary debate on dupes. The file is a file from stlport library and it has extern "C++" in its new version. I cannot change that. In the earlier version, it was extern "C". My question has remained on making this link with extern "C++" usage. I don't see many using extern "C++" and is also hardly googleable. Thanks. – Sammy Jul 19 '18 at 22:00
  • Also to add, if I do not call objApp.init() I do not get the Linker error (Question/observation appended). – Sammy Jul 19 '18 at 22:06
  • 1
    @sammy: That's probably dead code elimination. Anyway, since you don't show anywhere where `std::set_my_handler` is actually defined (and not just declared), there's no obvious reason to believe that the linker is mistaken. (In other words, I can't find that function either.) A [mcve] might be helpful. – rici Jul 19 '18 at 22:46
  • Thanks for pointing this out, I am trying to find the "definition" of set_my_handler in code. It could be that internally it is disabled and we can only see the declaration. I get the linker error "only" when the caller calls the function objApp.init() (inside which the set_my_handler is called). This is when the linker will link the symbols and not otherwise. Upvoted your answer. @rici – Sammy Jul 19 '18 at 23:29

0 Answers0