470,850 Members | 1,371 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Combining C and C++ codes

Hi all,

I have an Opencv face detection code that is written in C. I am currently doing a face detection project that makes use of Euresys frame grabbers. The problem is the image acquisition codes for Euresys are all written in C++, and i have to pass the frames captured to the face detection program for real time face tracking.

Will it be feasible to combine C and C++ codes?
Sep 24 '06 #1
1 7228
9,065 Expert Mod 8TB
Yes it is possible, but you will have a few problems to overcome.

1. C can not use classes (seems obvious but I thought I'd mention it).

2. C and C++ use different function calling conventions. C++ can be told use the C calling convention but C can not use the C++ calling convention.

You will often see code like this in a header file

Expand|Select|Wrap|Line Numbers
  1. #ifdef  __cplusplus
  2. extern "C" {
  3. #endif
  5. // Header file definitions
  7. #ifdef __cplusplus
  8. }
  9. #endif
This is used in a header file that contains the definitions for a C source file. All C++ compilers define __cplusplus when compiling C++ source files so basically what these directives do is tell the C++ compiler to use C calling convention for anything between the { and }. This allows the C++ and C csource code to call these functions.

There are many tricks to pull with this, for instance you can declare a function in a C++ file with C calling convention, then C code can call that function but because it is in a C++ file it can use C++ features, like access to classes.

I have worked on several projects that have mixed C and C++ code, it just takes a little care.
Sep 24 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by alwayswinter | last post: by
3 posts views Thread by hurry | last post: by
74 posts views Thread by Beany | last post: by
5 posts views Thread by Tristan Miller | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.