Domanda

I'm experimenting with some sample projects for the stm32f4. I would like to build on some of these with some c++ code.

If I add a cpp file, the ide seems to correctly recognise it as such. And if I set language to auto (extension based) my cpp file will build, with a class in it, great.

But can I connect between them? Everything compiles (no warnings), but it fails at linking if I call the cpp function from c (no definition for )

Is there a way to call cpp from c and c from cpp?

Thanks

È stato utile?

Soluzione

Yes, it's possible. However, you must explicitly tell the C++ that a function is a C function. You do this by declaring it as follows:

extern "C"
{
   void my_function(void);
}

To ensure that header files work properly under both C and C++, they are typically written as:

#ifdef __cplusplus
extern "C"
{
#endif
   void my_function(void);
#ifdef __cplusplus
}
#endif
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top