We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Anyone tried C++ yet. It seems to be available. I tried the following. iostream with no extension is available but the source file won't open it for some reason. On the last line of output window it says no C++ support available.
I was just curious to see how it compared to a C "hello C world"
But gave up.
#include <iostream> int main(void) { cout << "hello C++ world"\n"; return 0; }
I tried to use the C++ as is. But got a whole bundle of error messages which wasn't very encouraging. Like your engineers I was also put off by reports of bloated code. I was primarily lead by curiosity. But I dont understand how you are using the C++ compiler, and how do you invoke it. You are using the compiler supplied in BIN30 right? I would appreciate a minimal program to kick me off, to get a taste of things.
This is the second post on C++, as you are the only person to respond, maybe you are also the only person actually using Keils C++ ?!
I've started a C++ Project for the STMicro ARM0 STR912. All files without (today) the STMicro Library are in C++. It works very vell.
I am not really sure how to help.
I have created a new project and it by default uses realview, which is a c++ compiler. I created 1 file, main.cpp as follows (not very usefull)
//main.cpp
class a { public: int b; };
main() { int x; a c; c.b = 1; x = c.b; c.b = x; }
// end main.cpp
It compiles fine.
What are the error messages that you are getting?
Robert, thanks you have already helped, but I need a shade more!
I have been upgrading the Keil over time and I had an idea that there was a problem even though with C things were fine. trying anything with C++ gave me lots of errors. Anyway, today I decided to remove all Keil, clean the registery and re-instal v3.03a.
Good news, I can now compile the piece of code you posted. So next I wanted to go a step further and tried the following code. But I get one error
Text1.cpp(10): error: #20: identifier "cout" is undefined
in the project workspace I have Startup.s (copyright c 2005-2006) and Retarget.C and main.cpp (I am not sure if this is correct)
iostream is now found, which didn't happen before, and the multitude of error's have gone away. This is at the end of my day and I post this hoping that there is something simple wrong. I notice that iostream seems to pull in every library and header file, I count about 60 files below iostream in the project workspace.
I would like to know if this should compile as is.
//main.cpp #include <iostream> class a { public: int b; }; main() { int x; a c; c.b = 1; x = c.b; c.b = x; cout << "hello C++ world\n"; return(0); } // end main.cpp
I don't know if it sould compile, but it does not in my environment (cout is not defined). I don't use printf (or sprintf) because it increases the code size too much. I can't imagine what an implementaion of cout might do to code size, nor do I think that comparing the size of code using cout to printf has any meaning.
I'd have to agree with Robert that this particular program tells you nothing about C versus C++, but rather only compares the I/O libraries.
Back in the day, many C++ implementations didn't have nearly the refinement in their standard I/O libraries that the C compilers had put in. Use of "cout" tended to drag in lots of library code that might not have been directly related. In contrast, many C compilers, especially embedded ones, will snoop the printf() format strings and include one of several variant printf() implementations based only on the features of printf() you actually use. As a result, it wasn't uncommon for a C++ "hello world" to be larger than a C "hello world".
But this observation has nothing to do with the relative benefits and drawbacks of the languages, nor is it a gauge for what the compiler will do with your actual code.
These days the C++ compilers probably do better thanks to restructuring their internal standard library implementations, but I haven't tried it in a while.
One test would be to run your actual code through the C++ compiler. Of course, if it's really C code, you'll get pretty much the same results as from the C compiler. If you rewrite the code to C++, then you've introduced the factor of the skill used to rewrite the code (presumably not necessarily high when you're just conducting this sorts of initial experiments) and whether or not it's really an apples-to-apples comparison. It's a common mistake to throw every C++ feature in the book at such a rewrite, but then the two programs don't have the same capabilities. The program that can do dynamic dispatch or update its target objects while operating has capabilities a statically-bound version does not, and will also be larger and slower -- regardless of which language is used for the version with all the dynamism.
My experience has been that C++ is just as efficient as C. And when you ask for more advanced features (virtual functions, etc), the implementation there is also about as efficient as you'd roll by hand. The real questions to ask are:
- what benefits will the new language features bring to my project
- do I have the C++ skills to make good use of the new language (and if not am I committed to acquiring them)
I mention the second point because C++ is considerably more subtle and difficult to use well than is C. An expert C programmer is not an expert C++ programmer, and that programmer will have to study to become so. (Some programmers in my experience don't really accept this; their ego says they're experts, and they think their expertise automatically extends to anything related to computers. The superficial similarity in the syntax of C and C++ misleads them into thinking the semantics are also similar.)
C++ is powerful, but it also can trip you up if you don't understand what it's doing and why. It expects you to make choices that other language designers made for you, so you have to understand both forms of a feature and the tradeoffs involved. C++ is like a chainsaw: powerful, useful, and if you're not in control of it it might jump off the log and lop your toes off.
Drew, Robert. I really appreciate your comments. I am primarily driven by curiosity. I appear to have a good install now because Robert confirms getting the same results.
One feature of C++ that would have made the last project cleaner is the C++ feature "overloading functions".
I have just compiled an existing program and it is slightly smaller, approx 2% using the C++ compiler.
Where from here? I will try to become more proficient in C, and to get a handle on C++, until now I didn't see the point. But I am definately impressed by what Robert has indicated what can be done. Can I do what he did, definately not (read envy!).
Maybe I should ask if we can purchase what he has done, it would sound like a good investment.
I do wish COUT would work and perhaps I will try to look into it but for now I have to be real and concentrate on what gives me a living which isn't C programming.
It would be nice if there could be a Keil C++ promotion to provide more examples etc. I really didn't know that this tool was even available until I read the compiler pdf.
Anyway thanks again!