|  | Slightly OT: Compilation question |  | |
| | | Bit Byte |  |
| Posted: Fri Jun 13, 2008 9:14 am Post subject: Slightly OT: Compilation question |  |
I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
Is there anything I need to be aware of (i.e. any hidden dangers etc) ? - I am thinking specifically about things like default ctors (perhaps) being generated by the compiler for things structs etc ... (not sure if this would pose a problem in it self, but I simply want to make sure I have not overlooked anything ... |
| |
| | | Joachim Schmitz |  |
| Posted: Fri Jun 13, 2008 10:07 am Post subject: Re: Slightly OT: Compilation question |  |
Bit Byte wrote:
| Quote: | I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation. Is there anything I need to be aware of (i.e. any hidden dangers etc) ? - I am thinking specifically about things like default ctors (perhaps) being generated by the compiler for things structs etc ... (not sure if this would pose a problem in it self, but I simply want to make sure I have not overlooked anything ... You need to be aware that in C++ there are a bunch more keywords, which |
might have been used in C as identifiers. There are several constructs in C that have different sematics in C++ Check Bjarne Stroustrup, "The C++ Programming Language", Appendix R.18 "Compatibility"
Bye, Jojo |
| |
| | | CBFalconer |  |
| Posted: Fri Jun 13, 2008 12:22 pm Post subject: Re: Slightly OT: Compilation question |  |
Bit Byte wrote:
| Quote: | I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
|
I think you will be disappointed. Just use a good C compiler with heavy checking enabled. For example (one line):
"gcc -W -Wall -ansi -pedantic -Wwrite-strings -Wfloat-equal -gstabs+ -ftrapv -O1"
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section.
** Posted from LINK ** |
| |
| | | Victor Bazarov |  |
| Posted: Fri Jun 13, 2008 1:23 pm Post subject: Re: Slightly OT: Compilation question |  |
CBFalconer wrote:
| Quote: | Bit Byte wrote: I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
I think you will be disappointed. Just use a good C compiler
|
The OP said he *intended* to port to C++. How is using a good C compiler help in that? And what disappointment can await anybody who needs to use a C++ compiler to do a port to C++?
| Quote: | with heavy checking enabled. For example [..]
|
V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
| |
| | | Pascal J. Bourguignon |  |
| Posted: Fri Jun 13, 2008 1:34 pm Post subject: Re: Slightly OT: Compilation question |  |
Victor Bazarov <v.Abazarov@comAcast.net> writes:
| Quote: | CBFalconer wrote: Bit Byte wrote: I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation. I think you will be disappointed. Just use a good C compiler
The OP said he *intended* to port to C++. How is using a good C compiler help in that? And what disappointment can await anybody who needs to use a C++ compiler to do a port to C++?
|
Right. I think reading documents such as: LINK and scanning the code for all these gotcha (either automatically when possible or manually) would be more helpful.
-- __Pascal Bourguignon__ |
| |
| | | Kenny McCormack |  |
| Posted: Fri Jun 13, 2008 4:33 pm Post subject: Re: Slightly OT: Compilation question |  |
In article <g2u3dd$tdp$1@news.datemas.de>, Victor Bazarov <v.Abazarov@comAcast.net> wrote:
| Quote: | CBFalconer wrote: Bit Byte wrote: I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
I think you will be disappointed. Just use a good C compiler
The OP said he *intended* to port to C++. How is using a good C compiler help in that? And what disappointment can await anybody who needs to use a C++ compiler to do a port to C++?
|
It's like this:
OP: I live in a tent - I would like to trade up to a house. Can you put me in touch with a good realtor?
CBF: A house? Snort! Can I interest you in some aluminum siding for your tent? |
| |
| | | Ian Collins |  |
| Posted: Fri Jun 13, 2008 6:26 pm Post subject: Re: Slightly OT: Compilation question |  |
Victor Bazarov wrote:
| Quote: | CBFalconer wrote: Bit Byte wrote: I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
I think you will be disappointed. Just use a good C compiler
The OP said he *intended* to port to C++. How is using a good C compiler help in that? And what disappointment can await anybody who needs to use a C++ compiler to do a port to C++?
Compiling as C with a high warning would be a fair first step. |
Porting clean C to C++ is much easier than porting mouldy old (possibly pre-standard) code. That way you know when you compile as C++, the errors are down to the language differences, not the code.
Fix the C, add tests then port to C++.
-- Ian Collins. |
| |
| | | CBFalconer |  |
| Posted: Fri Jun 13, 2008 6:55 pm Post subject: Re: Slightly OT: Compilation question |  |
| |  | |
Victor Bazarov wrote:
| Quote: | CBFalconer wrote: Bit Byte wrote:
I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
I think you will be disappointed. Just use a good C compiler
The OP said he *intended* to port to C++. How is using a good C compiler help in that? And what disappointment can await anybody who needs to use a C++ compiler to do a port to C++?
|
The point is that he should abandon the port. The languages are different, and nasty sneaky mistakes are likely to arise. He can always call C code from C++, so he loses nothing except the pain of porting.
In addition, why do you cut off my sentences so they are misinterpreted. What I wrote was:
----------- begin requote -----------
| Quote: | I think you will be disappointed. Just use a good C compiler with heavy checking enabled. For example (one line):
"gcc -W -Wall -ansi -pedantic -Wwrite-strings -Wfloat-equal -gstabs+ -ftrapv -O1" ------------ End requote ------------ |
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section.
** Posted from LINK ** |
| |
| | | Martin Ambuhl |  |
| Posted: Fri Jun 13, 2008 8:55 pm Post subject: Re: Slightly OT: Compilation question |  |
| |  | |
Bit Byte wrote:
| Quote: | I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
|
You will get more strict compilation if the code is intended to be C++. You will get broken and incorrect translation in the code is intended to be C.
| Quote: | Is there anything I need to be aware of (i.e. any hidden dangers etc) ?
|
Yes. C and C++ are different languages with different syntax and different semantics. Treating C code as if it were C++ is asking for trouble.
| Quote: | - I am thinking specifically about things like default ctors (perhaps) being generated by the compiler for things structs etc ... (not sure if this would pose a problem in it self, but I simply want to make sure I have not overlooked anything ...
|
If your target is a language other than C (such as C++), you probably should do a real rewrite. The code you have may have taken advantage of features of C which have semantic differences in C; it may have taken advantage of features specific to its original environment not guaranteed to work even with C. And if there is any point at all in your plan to move it to C++, it for sure takes advantage of _none_ of the possible reasons you might have for doing so. Unless you are willing to do a rewrite taking advantage of real differences between C and C++, a plan to move to C++ just looks like adecade-late exercise in fad-following. |
| |
| | | Keith Thompson |  |
| Posted: Sat Jun 14, 2008 1:15 am Post subject: Re: Slightly OT: Compilation question |  |
| |  | |
Paul Hsieh <websnarf@gmail.com> writes:
| Quote: | On Jun 13, 4:14 am, Bit Byte <r...@yourbox.com> wrote: I have some legacy C code that I intend to port over (eventually) to C++. As a first step, I am thinking of renaming all of the *.c files to *.cpp, so I can benefit from the "more strict" C++ compilation.
Is there anything I need to be aware of (i.e. any hidden dangers etc) ?
Apparently sizeof has an actually different meaning in C++. I have not run into a case where I needed to investigate this myself as of yet.
|
No, sizeof means the same thing; it yields the size in bytes of its operand.
Some operands may have different sizes in C than in C++; for example, sizeof 'x' yields sizeof(int) in C and 1 (sizeof(char)) in C++. But that's a difference in the meaning of 'x', not in the meaning of sizeof.
[...]
| Quote: | One of the main differences is that C++ is more type strict, in particular void * is its own type and is not compatible with other pointer types -- you have to explicitly cast them to the types you are intending to use them as before accepting a coercion.
|
More or less. But void* is a distinct type in C. The difference is that C permits implicit conversions to and from void* in more cases than C++ does.
| Quote: | It also forces you to be more exact in function declarations. This I found to be the biggest actual source code impact, as it basically forces you to cast all mallocs.
|
Right (but it's usually better practice to use new and delete in C++ anyway, or some STL type that manages memory for you).
| Quote: | In C sometimes you could get away with declaring the prototype with no parameters, then the implementation and call sites with some specific parameters, which can lead to a kind of tricky way of doing polymorphic parameter passing -- I think this fails in C++, because C+ + needs to know the exact type of the function at time it is declared.
Other than that, usually you just find that C++ has stricter warnings.
|
At least a couple of people have posted pointers to good sources of information about the incompatibilities between C and C++.
-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| |
| Page 1 of 5 .:. Goto page 1, 2, 3, 4, 5 Next | |
|
|