Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Using C and C++ functions in the same program - how to do it

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 as below, or enter http://melksh.am/nnnn for individual articles
C051 - C and C based languages - C++ - General
  [4355] C++ in 2 days - (2014-12-18)
  [4335] Flexible public courses - residential or commuting, programming newcomer or experienced, C or C++ - (2014-11-30)
  [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)
  [2851] Further C++ material - view new or old - (2010-07-04)
  [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)

C239 - C and C based languages - Putting it all together
  [4559] When do I use the this keyword in C++? - (2015-10-29)
  [4374] Test driven development, and class design, from first principles (using C++) - (2014-12-30)
  [4326] Learning to program - comments, documentation and test code - (2014-11-22)
  [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)
  [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)


Back to
Separating groups of variables into namespaces
Previous and next
or
Horse's mouth home
Forward to
Throwing your own exception in C++, and catching it
Some other Articles
Well House Price list, January to March 2011
Belfast, on a cold November evening
Throwing your own exception in C++, and catching it
Using C and C++ functions in the same program - how to do it
Separating groups of variables into namespaces
Pictures from the Birkenhead to Belfast crossing
On the way to the course this week
Comments in and on Perl - a case for extreme OO programming
Melksham Carnival - getting ready for 2011
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3067_Usi ... do-it.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat