If you have some native C functions that you want to include in your C++, can you do so? The answer is a - slightly reserved - yes.
Firstly, you need to have your main program in C++ rather than being one of the C elements. Then you need both the C and C++ compilers to be from the same family / vendor and the same release, in order to avoid the combined code trying to call in two completely different sets of libraries. And finally you need to declare that the C functions actually are to be treated as C rather than as C++ at function prototype time.
I included an example, showing these, on an example on the course that finished earlier today ... you can see the C++ calling program
[here] - and note the prototype:
extern "C" int avg(int, int);
although there's nothing special about the call to the function itself:
int num = avg(8, 24);
The C function is
[here] ... and here's the compile, build sequence:
wizard:antrim graham$ gcc -c ave.c
wizard:antrim graham$ g++ -o Ns Ns.cpp ave.o
wizard:antrim graham$
This mixing of languages is a subject that comes up - occasionally - with many languages; you'll find me looking at it on Python, Lua and Tcl courses too, but only very briefly, and on public courses I'll only pull up examples "as required". The rule about the same family of compilers for the two languages (or for the compiler in the case of a scripting language) applies universally to avoid issues with multiple library loads and conflicts, and there's often issues with members being renamed with something like an extra underscore, or being "mangled" in order to avoid name conflicts across the various elements at load time.
(written 2010-11-24)
Associated topics are indexed under
C239 - C and C based languages - Putting it all together [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
[3252] C++ - unknown array size, unknown object type. Help! - (2011-04-17)
[3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
[2851] Further C++ material - view new or old - (2010-07-04)
[2674] Make and makefiles - a commented example to help you learn - (2010-03-12)
[2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
[2646] Compile but do not run PHP - syntax check only - (2010-02-22)
[1181] Good Programming practise - where to initialise variables - (2007-05-09)
[945] Code quality counts - (2006-11-26)
[925] C++ - just beyond the basics. More you can do - (2006-11-14)
[836] Build on what you already have with OO - (2006-08-17)
C051 - C and C based languages - C++ - General [3809] Dwarf Exception Unwind Info - (2012-07-20)
[3587] C++ Courses - do I get official certification at the end of my Well House course? - (2012-01-20)
[3250] C++ - how we teach the language and the concepts behind the language - (2011-04-17)
[3155] Rake - a build system using code written in Ruby - (2011-02-03)
[3129] Extra courses - C and C++ - (2011-01-12)
[3052] Getting your C++ program to run - (2010-11-15)
[2763] Our C and C++ training course are on Open Source platforms - (2010-05-13)
[2577] Complete teaching example - C++, inheritance, polymorphism - (2010-01-15)
[2536] All the Cs ... and Java too - (2009-12-13)
[2504] Learning to program in ... - (2009-11-15)
[2370] C++, Python, and other training - do we use an IDE - (2009-08-21)
59b7
Some other Articles
Well House Price list, January to March 2011Belfast, on a cold November eveningThrowing your own exception in C++, and catching itUsing C and C++ functions in the same program - how to do itSeparating groups of variables into namespacesPictures from the Birkenhead to Belfast crossingOn the way to the course this weekComments in and on Perl - a case for extreme OO programmingMelksham Carnival - getting ready for 2011