Don't combine the two in the same file. Writing C that compiles as C++ leads C people to yell at you, and vice versa. Instead, make a little C library, and have your C++ link against it. The only thing you need to do then is to add
#ifdef __cplusplus
extern "C" {
#endif
At the beginning of the header file for the C lib, and
#ifdef __cplusplus
}
#endif
At the end.
It's easy, and pretty, too: Create a Makefile
, since you're using gnu make, this is really easy:
program: cstuff.o program.o
With that, running make
will issue the following commands:
cc -c cstuff.c
g++ -c program.cpp
cc cstuff.o program.o -o program
So a directory listing will have 4 files: program.cpp
cstuff.c
cstuff.h
and Makefile
.
cstuff.h
contains all your structure definitions, and function prototypes, along with that extern "C"
stuff,
cstuff.c
is self-evident, and
program.cpp
begins with #include "cstuff.h"
can call the functions listed in the header file, and has all the C++-ey goodness you love.