Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compile C Code With A CPP Compiler?

Reply
Thread Tools

Compile C Code With A CPP Compiler?

 
 
entropy123
Guest
Posts: n/a
 
      09-30-2003
Hey all,

I'm working with some legacy C code and I would like to compile it as
a CPP file.

I get the following error message:

driver.cpp:87: cannot convert `void *' to `GenericStruct *' in
assignment

Reading through the web I've come across vague references to the
'void' issue between C and C++, I don't know C++ well and would
appreciate any pointers or references which might help me out.

Thanks!

entropy
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      09-30-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (entropy123) wrote in
news:(E-Mail Removed) om:

> I'm working with some legacy C code and I would like to compile it as
> a CPP file.


To what end?

> I get the following error message:
>
> driver.cpp:87: cannot convert `void *' to `GenericStruct *' in
> assignment


I believe C++ requires explicit casts on void pointers, you might ask this
in C++.

> Reading through the web I've come across vague references to the
> 'void' issue between C and C++, I don't know C++ well and would
> appreciate any pointers or references which might help me out.


Two approaches: (1) after finding out the C++ appropriate method of
converting void pointers to other types, fix-up all such conversions with
casts, (2) off-topic but you might find that there is a compiler override
switch to force the C++ compiler to theat a .cpp file as a .c file.

The latter has the benefit of allowing you to use one compiler for both
your C and C++ sources but won't allow you to add C++ features to said
files.

I'm not sure what benefit compiling a true C file as a .cpp file with a
C++ compiler will give you. Oddly enough, I have had to do that on my
current procject because it was once thought we'd convert drivers to C++.
Hence, I have told our compiler (Diab) to treat my .cpp files as .c files.
I've also wrapped the contents of the entire file with:

#ifdef __cplusplus
extern "C" {
#endif

/* File */

#ifdef __cplusplus
}
#endif

--
- Mark ->
--
 
Reply With Quote
 
 
 
 
dis
Guest
Posts: n/a
 
      09-30-2003
"entropy123" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...

> Hey all,
>
> I'm working with some legacy C code and I would like to compile it as
> a CPP file.
>
> I get the following error message:
>
> driver.cpp:87: cannot convert `void *' to `GenericStruct *' in
> assignment
>
> Reading through the web I've come across vague references to the
> 'void' issue between C and C++, I don't know C++ well and would
> appreciate any pointers or references which might help me out.


Your c++ related question is best answered in one of the c++ specific
newsgroups, e.g. comp.lang.c++.

[OT] Without seeing the actual code, my best guess would be that you need to
cast the expression of type void* to type GenericStruct*, as no implicit
conversion from void* to GenericStruct* exists under C++. [/OT]


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      09-30-2003
"entropy123" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hey all,
>
> I'm working with some legacy C code and I would like to compile it as
> a CPP file.


'CPP file' is not a concept defined by either the
C or C++ languages. Did you mean you wanted to
translate a file originally written in C with
a C++ compiler? This causes me to ask, why?

Note that some C constructs
are not valid in C++, and vice versa.

Also note that C++ is not topical here.

> I get the following error message:
>
> driver.cpp:87: cannot convert `void *' to `GenericStruct *' in
> assignment


This is a valid diagnostic for a C++ program, but not
for a C program.

> Reading through the web I've come across vague references to the
> 'void' issue between C and C++,


You cannot learn either language, or the differences,
by surfing the web. You need books.

> I don't know C++ well


Books!

>and would
> appreciate any pointers or references which might help me out.


I point and refer you to C++ books:
http://www.accu.org/bookreviews/publ...nner_s_c__.htm

and address your immediate problem:

C allows the conversion of 'void*' to and from any
other pointer type without a cast. C++ requires
a cast. (I'm guessing this is happening with an
invocation of 'malloc()')

#include <stdlib.h>

int main()
{
struct GenericStruct
{
/* whatever */
} the_data;

GenericStruct* gsp = (GenericStruct*)malloc(sizeof the_data);
/* whatever */
free((void*)gsp);
return 0;
}

Further discussions about this belong in a C++ group,
or the 'alt.comp.lang.learn.c-c++' group, where both
langs are topical.

Followups set.

-Mike


 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-01-2003
In <(E-Mail Removed) > (E-Mail Removed) (entropy123) writes:

>I'm working with some legacy C code and I would like to compile it as
>a CPP file.


This is a brain dead idea, unless you're both a C and C++ expert and you
know perfectly well what you're doing.

>I get the following error message:
>
>driver.cpp:87: cannot convert `void *' to `GenericStruct *' in
>assignment
>
>Reading through the web I've come across vague references to the
>'void' issue between C and C++, I don't know C++ well and would
>appreciate any pointers or references which might help me out.


1. By what kind of logic have you decided that comp.lang.c is the right
place for asking C++ questions? Do you also post your C questions to
comp.lang.c++ ?

2. If you don't know C++ well, you don't use a C++ compiler (except for
C++ learning purposes).

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
entropy123
Guest
Posts: n/a
 
      10-01-2003
Thanks for the replies. I spent all summer with C and I'm just
starting to learn C++. The idea with the legacy code is to convert it
from C/bison to C++. Most of the SRC files are written in "C" as well
as the driver. I was thinking if the driver is compiled in C++ then I
could start writing additional code in C++ without worry.

As I understand the situation, C programs may be compiled as C++, but
generally not the other way around.

Inasmuch as the replies involved C++ language I have no clue, but I do
own D&Ds C++ book which I've found excellent at least through the
first 100 easy pages..

Sorry for being OT.

entropy
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      10-01-2003
In <(E-Mail Removed) > (E-Mail Removed) (entropy123) writes:

>As I understand the situation, C programs may be compiled as C++, but
>generally not the other way around.


Your understanding is completely wrong. If you know what you're doing,
it is possible to write C programs that can be compiled with C++
compilers, but a well written, non-trivial C program is highly unlikely
to be acceptable to a C++ compiler.

C and C++ have a common subset. It doesn't make much sense to write
programs using only this common subset: they would be considered as
badly written from both the C and C++ point of view.

Until you *fully* understand these issues, don't try to compile C code
with a C++ compiler, unless you love shooting yourself in the foot.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: (E-Mail Removed)
 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      10-01-2003
"entropy123" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Thanks for the replies. I spent all summer with C and I'm just
> starting to learn C++. The idea with the legacy code is to convert it
> from C/bison to C++.


Again, why? If you're wanting to learn C++, the best
way imo is to write your C++ programs from scratch.
C and C++ syntax is similar enough, but the behavior
of each is (often subtly) different enough, that what
you're attempting will not only prevent learning,
but cause you to 'learn' things which are not correct.

> Most of the SRC files are written in "C" as well
> as the driver. I was thinking if the driver is compiled in C++ then I
> could start writing additional code in C++ without worry.


What has that to do with the C code?

>
> As I understand the situation, C programs may be compiled as C++, but
> generally not the other way around.


Then you don't understand. See Dan's reply.

>
> Inasmuch as the replies involved C++ language I have no clue,


And you should not expect anyone here to have any 'clues'
about C++ either. This group is about C. Some folks here,
including myself, have at least some understanding of C++,
but we don't discuss it here, as I pointed out in my previous
reply. I also referred you to a group where both languages
*are* topical.

>but I do
> own D&Ds C++ book which I've found excellent at least through the
> first 100 easy pages..


I think that's a pretty decent book, although there are others
I consider superior (it's a good idea to have more than one
book about a subject you're learning anyway).

>
> Sorry for being OT.


Here's the link to info telling what the topic here is:
http://www.angelfire.com/ms3/bchambl...me_to_clc.html

The C++ groups have similar documents, check them out
before posting there, either. You can find these documents
by perusing the posts in those groups. Links to them
are posted periodically, and are contained in the .sigs
of some of the regulars.


-Mike


 
Reply With Quote
 
entropy123
Guest
Posts: n/a
 
      10-02-2003
"Mike Wahler" <(E-Mail Removed)> wrote in message news:<oYDeb.11339
>
> Again, why? If you're wanting to learn C++, the best
> way imo is to write your C++ programs from scratch.


Agreed, but the project relies upon about 10 years worth of legacy 'C'
code. I've been working in C, and, after a few conversations, decided
C++ is a better longterm decision for the code.

'main' is in a *.c file with approximately 15 subordinate *.c files.
My understanding is that 'C' will compile just fine in 'C++' but not
vice versa. As it looks like C++ will be a better programming language
for this project I need to start moving the legacy 'C' code to 'C++'.

If the changes to the *.c driver are small it will all be worth it, if
not...

> Most of the SRC files are written in "C" as well
> > as the driver. I was thinking if the driver is compiled in C++ then I
> > could start writing additional code in C++ without worry.

>
> What has that to do with the C code?
>
> >
> > As I understand the situation, C programs may be compiled as C++, but
> > generally not the other way around.

>
> Then you don't understand. See Dan's reply.
>
> >
> > Inasmuch as the replies involved C++ language I have no clue,

>
> And you should not expect anyone here to have any 'clues'
> about C++ either. This group is about C. Some folks here,
> including myself, have at least some understanding of C++,
> but we don't discuss it here, as I pointed out in my previous
> reply. I also referred you to a group where both languages
> *are* topical.
>
> >but I do
> > own D&Ds C++ book which I've found excellent at least through the
> > first 100 easy pages..

>
> I think that's a pretty decent book, although there are others
> I consider superior (it's a good idea to have more than one
> book about a subject you're learning anyway).
>
> >
> > Sorry for being OT.

>
> Here's the link to info telling what the topic here is:
> http://www.angelfire.com/ms3/bchambl...me_to_clc.html
>
> The C++ groups have similar documents, check them out
> before posting there, either. You can find these documents
> by perusing the posts in those groups. Links to them
> are posted periodically, and are contained in the .sigs
> of some of the regulars.
>
>
> -Mike

 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      10-02-2003
(E-Mail Removed) (entropy123) wrote in
news:(E-Mail Removed) om:

> My understanding is that 'C' will compile just fine in 'C++' but not
> vice versa.


Not true. All your variables with names like 'new', 'class', etc. will
cause syntax errors.

--
- Mark ->
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code still compile when there is a mis-match in .h and .cpp yinglcs@gmail.com C++ 4 02-21-2007 05:41 PM
Free online test in C, CPP / Placement papers / CPP,C Interview Questions www.hitechskill.com C++ 0 04-09-2006 10:53 AM
when i compile the cpp file(cmdargs.cpp) int main(int argc, wchar_t* argv[]) Vinu C++ 9 05-05-2005 04:11 AM
Method inlined in source1.cpp and called in source2.cpp Alex Vinokur C++ 7 11-15-2004 09:14 PM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM



Advertisments