Calling C++ Defined Functions from C source

Came across this one today, I’m sure I’ve hit it before but of course I didn’t remember. The worst part of the problem is that the error message is not very descriptive (Unresolved external refrence) but the solution is nice and easy.

------------------
foo.h:
------------------
#ifdef __cplusplus
extern "C" {
#endif 

int foo( int ); 

#ifdef __cplusplus
}
#endif 

------------------
foo.cpp
------------------
#include "foo.h" 

int foo( int i ) { ... } 

------------------
main.c
------------------
#include "foo.h" 

int main() { foo(1); }

Leave a Reply

Your email address will not be published.