Call a C function from C++ code
Compile the C code like this:
gcc -c -o somecode.o somecode.c
Then the C++ code like this:
g++ -c -o othercode.o othercode.cpp
Then link them together, with the C++ linker:
g++ -o yourprogram somecode.o othercode.o
You also have to tell the C++ compiler a C header is coming when you include the declaration for the C function. So othercode.cpp
begins with:
extern "C" {#include "somecode.h"}
somecode.h
should contain something like:
#ifndef SOMECODE_H_ #define SOMECODE_H_ void foo(); #endif
(I used gcc in this example, but the principle is the same for any compiler. Build separately as C and C++, respectively, then link it together.)
Let me gather the bits and pieces from the other answers and comments, to give you an example with cleanly separated C and C++ code:
The C Part:
foo.h:
#ifndef FOO_H#define FOO_Hvoid foo(void);#endif
foo.c
#include "foo.h"void foo(void){ /* ... */}
Compile this with gcc -c -o foo.o foo.c
.
The C++ Part:
bar.cpp
extern "C" { #include "foo.h" //a C header, so wrap it in extern "C" }void bar() { foo();}
Compile this with g++ -c -o bar.o bar.cpp
And then link it all together:
g++ -o myfoobar foo.o bar.o
Rationale:The C code should be plain C code, no #ifdef
s for "maybe someday I'll call this from another language". If some C++ programmer calls your C functions, it's their problem how to do that, not yours. And if you are the C++ programmer, then the C header might not be yours and you should not change it, so the handling of unmangled function names (i.e. the extern "C"
) belongs in your C++ code.
You might, of course, write yourself a convenience C++ header that does nothing except wrapping the C header into an extern "C"
declaration.
I agree with Prof. Falken's answer, but after Arne Mertz's comment I want to give a complete example (the most important part is the #ifdef __cplusplus
):
somecode.h
#ifndef H_SOMECODE#define H_SOMECODE#ifdef __cplusplusextern "C" {#endifvoid foo(void);#ifdef __cplusplus}#endif#endif /* H_SOMECODE */
somecode.c
#include "somecode.h"void foo(void){ /* ... */}
othercode.hpp
#ifndef HPP_OTHERCODE#define HPP_OTHERCODEvoid bar();#endif /* HPP_OTHERCODE */
othercode.cpp
#include "othercode.hpp"#include "somecode.h"void bar(){ foo(); // call C function // ...}
Then you follow Prof. Falken's instructions to compile and link.
This works because when compiling with gcc
, the macro __cplusplus
is not defined, so the header somecode.h
included in somecode.c
is like this after preprocessing:
void foo(void);
and when compiling with g++
, then __cplusplus
is defined, and so the header included in othercode.cpp
is now like that:
extern "C" {void foo(void);}