467,923 Members | 1,479 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

mixing C++ with C

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


Jul 22 '05 #1
  • viewed: 1615
Share:
4 Replies
Cristian Tota wrote:
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.

Jul 22 '05 #2

"Cristian Tota" <cr***********@vion-software.ro> wrote in message
news:bq**********@ally.taide.net...
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-lit...c-and-cpp.html

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

HTH,
-Mike
Jul 22 '05 #3
On Thu, 4 Dec 2003 01:17:05 +0200, "Cristian Tota"
<cr***********@vion-software.ro> wrote:
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!

Jul 22 '05 #4
Cristian Tota wrote:
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.

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by bergel | last post: by
6 posts views Thread by Russell E. Owen | last post: by
reply views Thread by Erik Max Francis | last post: by
4 posts views Thread by Rudolf | last post: by
1 post views Thread by Marc Cromme | last post: by
2 posts views Thread by Dan | last post: by
reply views Thread by dhruba.bandopadhyay | last post: by
28 posts views Thread by ziman137 | last post: by
3 posts views Thread by jason | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.