Shop OBEX P1 Docs P2 Docs Learn Events
SimpleIDE Question — Parallax Forums

SimpleIDE Question

ajwardajward Posts: 1,130
edited 2015-01-08 12:55 in Propeller 1
Hi All...

I've been working with my BOE Bot after replacing the Stamp BOE with a Prop board. I'm using SimpleIDE to program the wee beastie in C. I kind of understand the project options... at least well enough that I don't crash the program (too often).

My question... I have the option of using a C compiler or C++. Is there any advantage to using one over the other? I've used both "successfully", albeit with small programs.

Curiously yours,

Amanda

Comments

  • Heater.Heater. Posts: 21,230
    edited 2015-01-05 06:38
    Depends. Which language do you want to use C or C++?

    As far as I can tell if you write C like code and compile it as C++ you will get the same code generated.

    Many will claim that C++ leads to bloated, huge and possible slower code. My experiments with gcc on the PC and Propeller show that this not true.

    You can for example write C code in an object oriented way, with objects being structs and methods being normal functions that get passed a pointer to the struct (the "object" they belong) to. Amazingly if you do that in the most obvious way in C and make the same objects in C++ the compiler will generate exactly the same code!

    What will lead to trouble is if you try and use the C++ standard library. Things like stream I/O take up a lot of room.

    I'd be inclined to work in C++ even if my code is only C like.

    Except possibly if you are using C code borrowed from elsewhere in your project.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-01-05 06:51
    If your question is "what are the advantages of the gcc compiler vs g++ compiler," then you might simply take a look at the differences between the two programs. I've looked into this with great interest and, if I can recall correctly, the only difference is that g++ automatically includes (at link time) the standard C++ library (the std:: namespace) and exception handling. If you then use any of code, your binary size blows up to an insane degree (as Heater mentioned above). Aside from that.... they're the same.

    The take away is then, don't use g++. The only advantage it gives you is std:: and exceptions and those two aren't usable on the Propeller - so save yourself some a small headache and compile all code (C and C++) with the gcc compiler.

    If, however, you are asking about the C vs C++ language differences.... heh heh... holy war time :P

    Personally, I like C++. If you're coming from a Spin background, I think you'll feel more comfortable since a C++ class more closely relates to a Spin file (object). Parallax's Simple library of course has numerous examples of good C-code. My PropWare objects and SRLM's libpropeller objects have good examples of C++ code. I've been working hard over the last many months to determine exactly what can, can not, should and should not be done in C++ to keep code size to a minimum, so do feel free to ask specific questions if you come up with any. The forum members here have helped me tremendously with mine.

    David
  • Heater.Heater. Posts: 21,230
    edited 2015-01-05 08:05
    Yes, SwimDude is right, forget about using the standard C++ library (std: ) and forget about using exceptions.
    If you are not using those g++ and c++ are equivalent as far as I can tell.

    Language differences aside there are some practical implications of using C++ vs C.

    For example, the C linker works with symbol names as you wrote them in your code. The C++ compiler does "name mangling" (To long to explain here, google it). The upshot of this is that if you are mixing up C and C++ modules in your program extra steps have to be taken to get the linker to stitch them together. It's not much but it's another thing to hassle about.

    Have a google for "extern "C""

    I think it's quite nice to use C++ if you want to make multiple instances of something. Rather in the style of Spin objects. If you don't need that then C will do.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-01-05 11:37
    jazzed wrote: »
    Really?

    Millions of Arduino users use C++/g++ with no problems whatsoever without std:: and exceptions, and SimpleIDE by default is setup the same way except that it requires normal C/C++ programming. C++ is valuable in educated hands, but no one has stepped up with a tutorial to show the way. SimpleIDE is a first step which really does make C/C++ simpler.

    It's just a question of how you'd like the error to show up. Would you rather see your linker blow up and yell "undefined reference to ____" or would you prefer propeller-load yell "doesn't fit in 32k". I prefer the linker because it's earlier (fail fast, fail early).

    David
  • David BetzDavid Betz Posts: 14,516
    edited 2015-01-05 12:05
    It's just a question of how you'd like the error to show up. Would you rather see your linker blow up and yell "undefined reference to ____" or would you prefer propeller-load yell "doesn't fit in 32k". I prefer the linker because it's earlier (fail fast, fail early).

    David
    I don't understand your comparison. If there was an undefined reference, no .elf file would be generated by the linker and hence propeller-load would have nothing to load.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-01-05 12:19
    David Betz wrote: »
    I don't understand your comparison. If there was an undefined reference, no .elf file would be generated by the linker and hence propeller-load would have nothing to load.

    If "the error" is defined as a Propeller user trying to use std:: or exceptions in their program, then I'm comparing two different ways that error can be reported. If the user uses g++ as SimpleIDE does, then the elf is correctly generated and propeller-load complains. However, if the user compiles with gcc, then error is reported as undefined references.

    Of course, you could probably use xmm models on the Propeller and then have full access to std:: and exceptions. I've always been intrigued by this but never tried it. For that reason though, I've been tempted to switch PropWare over to g++ and then have some options to enable inclusion of std:: and exceptions (because, of course, I would disabled them by default).
  • David BetzDavid Betz Posts: 14,516
    edited 2015-01-05 13:11
    If "the error" is defined as a Propeller user trying to use std:: or exceptions in their program, then I'm comparing two different ways that error can be reported. If the user uses g++ as SimpleIDE does, then the elf is correctly generated and propeller-load complains. However, if the user compiles with gcc, then error is reported as undefined references.

    Of course, you could probably use xmm models on the Propeller and then have full access to std:: and exceptions. I've always been intrigued by this but never tried it. For that reason though, I've been tempted to switch PropWare over to g++ and then have some options to enable inclusion of std:: and exceptions (because, of course, I would disabled them by default).
    Okay, I see what you mean. I guess the program builds okay but doesn't have enough memory to run when using g++. I'd like to see the generated .elf file to determine how it is failing in a way that causes propeller-load not to be able to load it. I can see that it would fail at runtime if it tried to allocate memory with too small a heap.
  • DavidZemonDavidZemon Posts: 2,973
    edited 2015-01-06 19:30
    Well, I was very wrong. I forgot that the linker actually throws the "region `hub' overflowed..." error. I'm now having trouble coming up with another good reason to use gcc over g++. Will run some tests tomorrow...
  • ajwardajward Posts: 1,130
    edited 2015-01-06 23:52
    Thanks for all the thoughtful responses everyone. I'll just tinker on and see what works best. ;-)

    @
  • ajwardajward Posts: 1,130
    edited 2015-01-08 11:01
    Just a followup in my adventures programming my modified Boe Boot with C.
    I was putting together a function to rotate the servo on a Ping kit. Every time I''d try to build the program, a warning popped up about some default value. I changed numerous things and still the warning. I Googled the error message for what seemed a "google" of times and nothing would rid me of the infernal warning.

    Then yesterday morning, oh-dark-thirty, I looked at the error one more time and something clicked. I changed the function declaration and name to int xxxxxx and everything worked wonderfully. The darn warning message was telling me all along what the problem was and I was too thick headed to see it! How dare it tell me what to do!!!!!

    Sigh! Going to my room now...

    Amanda
  • Heater.Heater. Posts: 21,230
    edited 2015-01-08 12:55
    ajward,

    Well, if you don't tell us the warning/error message you got or show us the code that generated it, we have no way to know what on Earth you are talking about.

    However, what you are describing is a normal state for a C/C++ developer.:)

    There is a good argument that all compiler warnings should be classed as errors. To that end the -Werror flag should be set on all compilations.
Sign In or Register to comment.