Assignment with ?=
LoopyByteloose
Posts: 12,537
In going through a lot of the code for CNC and 3D printers I am frequently seeing ?= and I presume it is shorthand for 'Maybe this value is right'. I can't find anything by searchin the internet and the list of standard operators doesn't include it for C or C++.
Comments
The only example I know in C that uses ? (ternary operator) is a conditional statement such as:
int a = 0;
int b = (a == 0) ? 1 : 0;
// if (expression) evaluates as true, b = 1, else b = 0.
The (a == 0) can be any variable or expression that returns non-zero (true) or 0 (false).
Got a link?
For example...
DEVICE ?= atmega328p
CLOCK = 16000000
PROGRAMMER ?= -c avrisp2 -P usb
OBJECTS = main.o motion_control.o gcode.o spindle_control.o coolant_control.o serial.o \
{The GNU Make Manual in PDF is 211 pages long.
I found ?= mentioned in Appendix A.}
?= is not a C operator. It is valid in a Makefile though and just means that the assignment is only done if the variable to the left doesn't already have a value.
Thanks, you explaination is clearer than Sec. 3.7 in the Make documentation.
I am a bit concerned that I cannot just read the related C code as a stand alone item and just ignore the Makefile. This makes sorting out a conditional compile a bit harder.
Hopefully in any big app the programmers have been kind enough to bury away all that stuff in low level utility functions in the program and you don't need to think about it much.
In small embedded apps that desire to be portable or support different devices that organization is not always possible and ifdef soup permeates everything.
Consider it a challenge
In other words, I have to learn what AVR Studio is doing, what the Arduino IDE is requireing, and what the actual code author is thinking.
Arduino is a muddle of many facets..... It may seem painless to the inocent new user, but there is a darkside of vast complexity.
It is a fractal of infinite complexity that no human being can comprehend in it's entire magnificence.
It is C++.
Yes, that's partially why C++ was invented. There is a whole Camel of other reasons for the rest of C++ behaviour though!
In PropellerGCC all the little differences between boards can be defined in the board type configuration file (.cfg) by using variable stuffing. No need to change the source between when it is used properly to set pins and variables, etc.... This allows lots of load-time source sharing without defines, etc.... In other language environments you have to go make a few to hundreds of lines of code changes to achieve the same result.
C++ makes it better though because on top of the ifdef soup you can mix in template soup, class soup, class hierarchy soup, multiple inheritance soup, polymorphism soup, exception soup, etc. It's almost impossible to find where the code is that actually does anything or which parts if it actually get run at any moment:)
If you start with #ifdef mentality, you will need it forever.
An example please.
I was curious so I thought I would ask the experts. In the Clang/LLVM compiler, written in C++, there are: 2736 #ifndef and 1050 #ifdef for a total of 3786 bits of ifdef soup.
Perhaps that's not so bad as they do have 8900 files. But it seems polymorphism does not get you out of the soup completely.
What have I learned?
A. Avoid reading code for C from platforms other than the Propeller.... even if the particular body of code is what I really want to learn about.
B. People learn C++ to break the platform independece of C code, and then add a Makefile and non-standard libraries to obscure even more.
C. I need to learn a lot more to code in C in the Propeller.
D. Everything I need to know should be in the Learn site, but it is not really finished.
E. I have to learn the standard C libraries and non-standard Propeller libraries, but not sure which does what in the best fashion.
I suppose I could just go back to Forth. I get the impression it might actually run at least as fast as C and maybe faster.
Or if I really want to learn C and C++, I might as well jump in both feet and learn AVR Studio 6, warts and all. or just program in Linux on my desktop.
After all, I am pretty much in the same pickle no matter which I choose.
I am actually making an effort to learn all the C libraries involved and to get into a C miindset with the Propeller. Maybe in a month or two I'll have acquired enough to get something done.
I still don't know if I can safely and completely ignore a Makefile when incuded. I guess the only way to find out is to completely master Make and completely understand the Makefile involved.
Start with C. All you need to learn C is in the recent ANSI editions of the famous book "The C Programming Language" by Kernighan and Ritchie. Commonly known as just "K&R" or "The White Book". I guess there are a billion C tutorials on the net to help.
I think you are right, until you are comfortable with the language it's probably best to experiment with it on your PC. All you need is your favourite editor, preferably with syntax highlighting, and the command line. "$ gcc -g -00 -std=C99 -o myProg myProg.c" is not so hard. You can have fun stepping through your code with the Data Display Debugger, ddd, should be available as a package for Debian.
That will get you up to speed on most of the standard lib stuff whilst you are at it.
Messing with super-mega.studio-this and awsome-ide-that and some weird platform like AVR or Propeller at the same time just confuses things and makes progress more hard work.
Best to stay away from C++ initially. No body understands that stuff.
Make is another story. It's been around forever and welded itself into the C ecosystem. Nobody understands that either. It's so annoying that there have been many attempts to make replacements that are easier. They just get worse...
I am just very fed up with my attempt to port the 3D printer code "for Arduino" to the Propeller. It is really AVR Studio code that sucks you into the whole 'AVR world'.
I will just keep poking along. I thought this would take a few weeks, but it may be a few months... maybe next year.
Mainly, I really need to learn out to apply the libraries to the Propeller. K&R will never provide that.
The prime reason I enjoy the BasicStamps, the SXes, and the Propeller with Forth, Spin, ASM, and PBasis have been that I didn't need the formalities of C.
If it all seems too hard, I can just study Chinese until I want to get away from that.
GEDIT is the text editor I am using and it has a highlight selection for C or C++, and may other languages... including Forth.
Clearly it has a lot of dependencies on AVR hardware and Arduino libraries and such. Not trivial.
My gut tells me that perhaps it's better to just use an Arduino to run that rather than waste a year of your life trying to convert it to the Propeller.
At least the Teacup code is supposed to be C and not C++.
I do have to admit that my selection criteria was rather naive. I just wanted something that claimed to fit into an ArduinoUNO code space and was in current development. This seemed practical at the time.
But I do learn a lot by comparison of the differences.
In trying to get all my 'ducks in a row' about the standard libraries for C and C++, I downloaded a PDF for the C++ Stardard Libraries as of 2011. OMG !!! over 1100 pages? The C Stardard Libraries are an appendix in the back of K&R.
I can't begin to fathom why SimpleIDE decided to bother with C++ at all. Seems to intended for complete general purpose computing systems on a much larger scale. Even then, Linux Torvalds won't use it.... but the Arduinos will use it.
http://it-ebooks.info/book/1255/
If the Raspberry Pi Foundation really wants to promote computer education in young people, it should downsize this tome to about 200 pages of what is actually 'need-to-know' informaton. Books that are too heavy to carry or even to deal with comfortably at a desk get put aside.
I really am disgusted with the Object Oriented Programing bloat.... worse than a Minoan labrynith with a raging minotaur.
Maybe you should have a message popup when users in SimpleIDE select C++ that says,
"|Are you serious? Do you really want to use C++. It is certainly not suited for new users."
They are promoting the use of Python....that really cuts down the size of the C++ manual you need.
Like I said, don't think about C++ for now. The C++ standard library is huge, pretty much none of it will be usable from within the confines of a Propeller.
Having said that C++ is usable on small systems. The Arduino shows that. You can in fact define classes and use objects in C++ and end up with byte for byte exactly the same binary code as if you had done a similar thing in C using hand made instance pointers. C++ comes from free with the GCC compiler so why not?
The Raspberry Pi guys are for sure not pushing C++ on children, I think there are laws against that sort of thing, they encourage Python or Scratch or pretty much anything else.
There is no "bloat" inherent in object oriented programming. I mean, think about any program you write, it has data (or state as they like to say), and it has has functions to operate on that state. There is your properties and methods. It is an object. It's source code is a "class" definition. OOP is only there to formalize what you might do with functions operating on data structures anyway.
That's not to say that OOP does not have issues though.
Not even all of C is really appropriate for the Propeller, but at least the early books avoided introducing those features to beginners and focused primarily on what C did with a limited set of somewhat universal standard libaries. Even then, it seems to have a bit too much focus on floating point for microcontrollers.
I am trying to ignore C++ as much as possible. I can't seem to find any books for the beginner that are less that 600 pages (maybe none less that 700). And therein lies the absurdity. Somewhere and somehow, through the boom in word processing software and desktop publishing, books on computing have gotten to absurdly large. And on line tutorials of beginners are rather hit and miss.
Nothing that I am aware of really exists in a 200-350 page range of introduction that thoughtfully hits upon the need-to-know.
++++++++++++++++++++
I just feel that real beginners are being abused by these big books that go on and on about abstract concepts and provide code example after example. One 700 page test even claims in its title that you can learn C++ in one hour per day. I can only imagine how many hours at one hour per day it would take to get through the book. I an a bit conflicted by the tiltle as I have actually considered SAMS a first-rate publisher.
http://www.informit.com/store/sams-teach-yourself-c-plus-plus-in-one-hour-a-day-9780672329418
At this point, a big part of the problem is the including of Object Oriented Programing in all the new software. I think that teaching OOP from the start is often counter-productive and adds a lot of complexity without fully making a case for the student programmer needing to master it.
+++++++++++
My dilemma is that I do have some C++ code written for the Arduinio that I am trying to read and make work in Tonokip_with_Serial, but I can't find any short and clear book or paper to explain the differences. Does anything exist?
While it seems like I am being too negative, I am learning and making progress. I just feel that if the Propeller is going to include C++, the hazards of big books and big documents need to be minimized by some guidance rather than just telling people to ignore the C++.
The issue of makefiles is best avoided by using SimpleIDE for project management as it was really intended for larger platforms and larger compiles. But people do run into them and need to know something. I did find a good general introduction to Make and Makefile in "How Linux Works: what every superuser should know" by Brian Ward; 2004. That has a Chapter 8 - Development Tools that is quite clear and short. Best of all, you can download a free PDF version on line.
Here is the publisher's page........
http://www.nostarch.com/howlinuxworks.htm
I feel your pain. Knowledge by the pound. Never mind the quality feel the weight
I have the original C++ book by Bjarne Stroustrup from the last century. It's a nice skinny little volume. It has grown hugely over the years but is probably still the best place to start. http://www.stroustrup.com/4th.html
You can see the same in the JavaScript language world where there are many huge books. All you need is Douglas Crockford's "Javascript The Good Parts". Which not only skips a pile of Smile in the language but points out it's unique features that make it more than just a stupid scripting language.
Then there is the "magic" of the Arduino. Where they use C++ but if you check the Arduino tutorial and reference pages they only talk about the simple things you need to know and bury all the mind bending complexity of C++. Brilliant. Perhaps eventually the Parallax needs the same level of documentation for C++ on the Propeller. It's a lot of work.
Don't get me started on OOP. Sure objects and such are nice. As we see in Spin. But this whole class based OOP paradigm is the biggest disaster foisted on the unsuspecting programmer ever. And C++ is one of the most contorted ways to tackle it.
Makefiles are great. If you don't have a life.
Anyway, your issues with porting Arduino code to the Propeller compound all the language / library complexity with a fundamental change in hardware. Single processor to parallel processor. Interrupt driven system to event driven. Hardware peripherals to software drivers. A mammoth task no matter what language.
http://www.pearsonhighered.com/educator/product/Tour-of-C-A/9780321958310.page
That seems much more attractive. Of course the same author's beginners text for C++ is 700+ pages.
It just seems that there is a real need for editors that might rein in authors that write about computer languages.
And if an author MUST publish a 700 page text, why not in 2 or more volumes? There are very good reasons that the 'pocket book' was so successful. One was cost, but another was the weight factor. People that want to study something in depth do appreciate has to NOT carry around a 5 kilo tome, and may not desire an e-book version.
++++++++++++++++++++++
I just read an explanation of Polymorphism...
That seems as confusing as everyday English. There is a nasty hazard with expansion of lexicon.
Here is an example of polymorphism in everyday English usage...
leave (v) -- [1] to depart, [2] to remain after departure (past tense - left)
leaf (n) -- [1] a component of a plant (plural - leaves)
left (adj) -- [1] a sideways direction (the opposite of right)
right (n) -- [1] correct (the opposite of wrong)
From what I can see the the prime objective of OOP is to have portable code. This is a lofty ambition for the beginner... it presumes that what they write will go into some vast corpus of code and be reusable forever by all humanity.
IN sum, OOP is somewhat idealistic and impractical as some higher authority has to maintain the quality and integrity of this ever growing body of code. Much easier to limit the lexicon and focus on application of that limited set.
If the publishers are pushing oversized books on C++ you could always go to the defining standards documents: Working Draft, Standard for Programming Language C++ http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf
Ah, but that document is 1350 pages long! And is is of course totally unreadable for normal people.
Explaining what that standard says and showing how to actually use the language is going to be even longer.
Microsoft could have stopped at MSDOS 3.1.
Word processing could have stopped with Wordstar or maybe, Wordperfect.
Spreadsheets could have stopped with Visicalc or Lotus
And of course I feel that C has never needed C++, C##, Objective C, C shell ... etc.
++++++++++++==
Bye, have to get back to something productive.