By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

mixing C++ with C

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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.