mixing C++ with C

C

Cristian Tota

Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project that
uses a given C interface, the rest of the project can be either in C or C++.
What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone could
speak from personal experience about this.

Thanks,
Cristian
 
D

Dan W.

Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project that
uses a given C interface, the rest of the project can be either in C or C++.
What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone could
speak from personal experience about this.

Thanks,
Cristian

stand-alone functions, you can wrap in

extern "C"
{
int foo(){}
}

If you need to expose classes to C, you can concatenate the class name
to the function names, make them extern "C" and pass to them an extra
static pointer to substitute the this pointer. Member variables become
file-scope static variables.
A few issues back of CUJ there was an article on state machines which
presented object oriented code techniques in C to go with it.
But, usually, for interfacing you'll probably just need interface
classes --i.e. just functions, probably static, which you can collect
into ,C files, which in turn call the C++ code.

Hope it helps.
Cheers!
 
M

Mike Wahler

Cristian Tota said:
Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project that
uses a given C interface, the rest of the project can be either in C or C++.
What would be the recomended design pattern in this case? C++ allows for a
better design, but integrating the code with the C code isn't straight
forward, it becomes rather messy. Code readability and maintenance are
important, since the project will grow in the future. Maybe someone could
speak from personal experience about this.

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

http://www.comeaucomputing.com/techtalk/#externc

HTH,
-Mike
 
E

E. Robert Tisdale

Cristian said:
I'd appreciate any thoughts on mixing C++ and C code.
I have a project that uses a given C interface.

What does that mean?
Do you mean that you have libraries implemented in C?
The rest of the project can be either in C or C++.
What would be the recommended design pattern in this case?
C++ allows for a better design
but integrating the code with the C code isn't straight forward.

If you say so.
It becomes rather messy.

It shouldn't.
Code readability and maintenance are important
since the project will grow in the future.

Try to use C++ for the "rest of the project".
Maybe someone could speak from personal experience about this.

If you have source code for your C code,
try to compile it with your C++ compiler.
It is *much* easier to link object code together
if they are all compiled with the same compiler.

If you don't have all of the C source code (just the library archives),
extern "C" can be used to *help* with linkage. Try using

extern "C" {
#include <file.h>
}

to include your C header files.

Use inline functions to define C++ [member] functions, operators
and constructors which call the "C interface" so that
no *direct* references to the C interface
appear in your application source code.
 
R

Rolf Magnus

Cristian said:
Hi,

I'd appreciate any thoughts on mixing C++ and C code. I have a project
that uses a given C interface, the rest of the project can be either
in C or C++. What would be the recomended design pattern in this case?
C++ allows for a better design, but integrating the code with the C
code isn't straight forward, it becomes rather messy. Code readability
and maintenance are important, since the project will grow in the
future. Maybe someone could speak from personal experience about this.

It might be a good idea to wrap a C++ class interface around the C API,
so that this API is used directly only in small parts of your program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top