I am trying to call an inline function defined in C from C++ code and I am getting an unresolved symbol error. Can I even do this?
In def.h:
#ifdef __cplusplus
extern "C" {
#endif
#include "types.h"
extern inline void foo();
#ifdef __cplusplus
}
#endif /* extern "C" */
In def.c:
inline void foo()
{
some code
}
Elsewhere in c++ code:
void bar()
{
foo();
}
And this is where I get an unresolved symbol error. Is there a way to make this compile? And if so, how will it work? Will the compiler replace the function call in my C++ code by the C definition? Eliminating functional calls is important as this is for an embedded environment.
Thank you.
Most compilers generate code on source file level; to generate code, the compiler must "see" the source of all functions for which it needs to generate code. Unless you have LTO/LTGC, you have to make the source of all inline
functions available in every file where they are called.
Try moving the source of the inlined function(s) into the corresponding header:
def.h:
#ifdef __cplusplus
extern "C" {
#endif
#include "types.h"
inline void foo()
{
some code
}
#ifdef __cplusplus
}
#endif /* extern "C" */
def.c:
Remove foo()
definition from this file (or remove this file).
Elsewhere in c++ code:
#include "def.h"
void bar()
{
foo();
}