C vs. C++

hello,

thanks a lot for your help. I have still some questions, we are builting a
VMEbus system to control a robot. I am using QNX because it is free and very
convinient.

however, for my program, I use C++ whereas, the drivers for the I/O boards
are in C language. does the compiler is able to link C and C++ together? or
does I have to program everything in C?


if anyone can help me on this one that’d great!!


thanks in advance!

Julian.

“julian” <julien_prevost@hotmail.com> wrote in message
news:a7g7nb$7de$1@inn.qnx.com

hello,

thanks a lot for your help. I have still some questions, we are builting a
VMEbus system to control a robot. I am using QNX because it is free and
very
convinient.

however, for my program, I use C++ whereas, the drivers for the I/O boards
are in C language. does the compiler is able to link C and C++ together?
or
does I have to program everything in C?


if anyone can help me on this one that’d great!!

C++ can call C code, the trick is the use extern C { }
around the prototype of the C function (check stdio.h for an example)

C can’t call C++ code (well it could but that’s black magic)

thanks in advance!

Julian.

Hi Julian…

You can program in both C and C++. Do as Mario suggested, but do compile
using the c++ fashion.

Usually I instantiate c++ classes in a main.c code as objects (main.c is
using C ofcourse), and you can always call C functions from c++.

Regards…

Miguel.

julian wrote:

hello,

thanks a lot for your help. I have still some questions, we are builting a
VMEbus system to control a robot. I am using QNX because it is free and very
convinient.

however, for my program, I use C++ whereas, the drivers for the I/O boards
are in C language. does the compiler is able to link C and C++ together? or
does I have to program everything in C?

if anyone can help me on this one that’d great!!

thanks in advance!

Julian.

my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work.

Miguel Simon
Research Engineer
School of Aerospace and Mechanical Engineering
University of Oklahoma
http://www.amerobotics.ou.edu/
http://www.saic.com

Typically what people do in their headers is:

#ifdef __cplusplus
extern “C” {
#endif


#ifdef __cplusplus
}
#endif

“Mario Charest” <goto@nothingness.com> wrote in message
news:a7gea8$bkm$1@inn.qnx.com

“julian” <> julien_prevost@hotmail.com> > wrote in message
news:a7g7nb$7de$> 1@inn.qnx.com> …
hello,

thanks a lot for your help. I have still some questions, we are builting
a
VMEbus system to control a robot. I am using QNX because it is free and
very
convinient.

however, for my program, I use C++ whereas, the drivers for the I/O
boards
are in C language. does the compiler is able to link C and C++ together?
or
does I have to program everything in C?


if anyone can help me on this one that’d great!!


C++ can call C code, the trick is the use extern C { }
around the prototype of the C function (check stdio.h for an example)

C can’t call C++ code (well it could but that’s black magic)


thanks in advance!

Julian.
\

I have done this. But it is a pain.

What I do is write a cover function for the C++ function or method you want
to call. But typecast (or whatever kind of cast you call it) the cover
function as extern “C”. You can still set up C++ calls inside that
function.


Bill Caroselli – 1(626) 824-7983
Q-TPS Consulting
QTPS@EarthLink.net


“Mario Charest” <goto@nothingness.com> wrote in message
news:a7gea8$bkm$1@inn.qnx.com

C++ can call C code, the trick is the use extern C { }
around the prototype of the C function (check stdio.h for an example)

C can’t call C++ code (well it could but that’s black magic)

Hi Julian

I am the biggest C++ enthusasist you’ll ever find. I use it (ALMOST) all
the time. However, I would add to the comment below, If size or execution
speed is absolutely essential to any program, avoid C++.

But by all means don’t be afraid to use whichever is best suited to the task
at hand. And don’t be afraid to mix languages.

EXAMPLE:
myFunction.c

int myTimeCriticalFunction ( )
{
// yadda yadda yadda
}

myMain.cc

extern “C” int myTimeCriticalFunction ( );
int main ( )
{
myTimeCriticalFunction();
}

will call a C function from a C++ program. If you look in the standard
headers you will see ‘#ifdef __cplusplus’ around all function prototypes.
This tells C++ programs that may include those headers that these are C
programs.

Good luck.


Bill Caroselli – 1(626) 824-7983
Q-TPS Consulting
QTPS@EarthLink.net


“Miguel Simon” <simon@ou.edu> wrote in message
news:3C9CAE0C.3FAD1C4B@ou.edu

Hi Julian…

You can program in both C and C++. Do as Mario suggested, but do compile
using the c++ fashion.

Usually I instantiate c++ classes in a main.c code as objects (main.c is
using C ofcourse), and you can always call C functions from c++.

Regards…

Miguel.

julian wrote:

hello,

thanks a lot for your help. I have still some questions, we are builting
a
VMEbus system to control a robot. I am using QNX because it is free and
very
convinient.

however, for my program, I use C++ whereas, the drivers for the I/O
boards
are in C language. does the compiler is able to link C and C++ together?
or
does I have to program everything in C?

if anyone can help me on this one that’d great!!

thanks in advance!

Julian.

my opinions are mine, only mine, solely mine, and they are not related
in any possible way to the institution(s) in which I study and work.

Miguel Simon
Research Engineer
School of Aerospace and Mechanical Engineering
University of Oklahoma
http://www.amerobotics.ou.edu/
http://www.saic.com