5 ms·
Awesome guide on how to get the best out of C++ on embedded systems. One thing I would add is: enable all the warnings you can and make them errors: "-Wall -We
by fest 10y ago
Awesome guide on how to get the best out of C++ on embedded systems.
One thing I would add is: enable all the warnings you can and make them errors: "-Wall -Wextra -Wpedantic -Werror".
I know it's hard to do in practice (who will want to fix the thousands of warnings in a well seasoned codebase?) but it's a good idea for new projects. Especially if team uses the same compiler (sometimes the code produces warning with newer gcc versions).
- jackhack 10y agoYes! This was a practice beat into me (not literally, but close) at my first programming gig in the 1980s. "A compile should not produce warnings! If the code producing the warning is truly intentional & deliberate, then grab a senior developer to review and ONLY if he agrees we'll issue a #pragma directive to tell the compiler to ignore it. But most of the time, (99%+) there's a way to do what you want without triggering a warning." Most of the time it was simple mistakes or laziness like a downcast conversion leading to loss of precision, etc. but occasionally on review we'd find somethign really stupid/dangerous like type mismatches on a pointer ("well, no, that index into a string should NOT be used to reference memory...") or a return before a free() call (memory leak), uninitialized values (yay for random behavior!). I thought sloppy, warning-riddled code was ancient history until last week, when I did a fresh install of R Studio on a server + a couple dozen packages, watching page after page of gcc compiler warnings scroll past.
- pjmlp 10y ago> I thought sloppy, warning-riddled code was ancient history until last week, when I did a fresh install of R Studio on a server + a couple dozen packages, watching page after page of gcc compiler warnings scroll past. Nah, just compile or use any Gtk+ based application.
- wott 10y agoOh yes, that is true. I launch every program from terminals and a terrible warning parade fills my terminals whenever it is a Gtk application. I came to think that the developers never actually launch their program this way and never the see the flow of warnings they trigger.
- RealityVoid 10y agoI might misunderstand what kind of warnings you are talking about, but I saw OP talking about compiler warnings during the build process and you about GTK application runtime warnings. They are different, and in the context of an application it's... well, not necessariliy good, but normal. Runtime warnings and unexpected input is at least input the programmer is aware of. I'd be more scared of an app that lacks runtime warnings.
- pjmlp 10y agoYep, but regarding my remark I actually meant both.
- mrkline 10y agoAuthor here - thanks! I left out advice like, "turn on all the warnings" because I was trying to focus on problems specific to embedded platforms. I'd strongly argue advice like that that is great regardless of what your target is.
- pksadiq 10y ago> One thing I would add is: enable all the warnings you can and make them errors: "-Wall -Wextra -Wpedantic -Werror". Note: GCC (and probably other compilers) for Embedded systems don't accept 'void' return type for 'main' function without warning. As it is allowed by standard (ie, it can be implementation defined), and as it is normally used as return type for 'main' function in Embedded programming, the compilation may fail at 'main', even if the code is strictly conforming to standards.
- matt_trentini 10y agoMy recollection is that 'void main' is illegal and always has been according to all versions of the C++ standard. (I'll check when I'm no longer on a phone unless someone beats me to it!) Further, to confuse the issue, you don't have to explicitly return anything from main even if it's defined as 'int main'.
- shakna 10y agoFrom Standard docs., 3.6.1.2 Main Function, It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main: int main() { / ... / } and int main(int argc, char* argv[]) { / ... / } In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run.If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings --- No version of the C or C++ specs have said void main is acceptable, (apart from the implementation-defined optional main for non-hosted systems). However, most compilers have it as a language-extension. A return statement in main was compulsory until C99, where the compiler was to assume return 0, if not specified. Some compilers had that behaviour already, however.
- planteen 10y agoWarnings and static analysis tools are good, but wrangling them across multiple tools, architectures, and coding standards is hard. Really hard. Different compilers will throw warnings for different things. Linting gets crazy. A customer might ask you to run your code against a MISRA ruleset. A different customer may have their own coding standard. The two standards may contradict each other in areas. Did you know that printf returns a value? Aggressive linting tools will throw a warning about an unchecked return value. So is printf("hello world?\n") wrong? And (void)printf("hello world\n") right? I don't know anymore.
- ktRolster 10y ago>And (void)printf("hello world\n") right? then of course, you find a tool that complains, "useless cast"
- planteen 10y agoYep!
- thr0w__4w4y 10y agoSerious question (I've been exactly the guy that planteen described -- wrangling code into submission for multiple static analysis tools and compilers, for warnings, MISRA compliance, etc.) So my question: do you remember what tool would complain about such a construct (e.g. casting return value of printf() to void)? Reason I ask is because printf() returns a value; the cast to void is explicitly and deliberately -- right or wrong -- saying, "It's OK, I got this. I am choosing to ignore it." So a static analysis tool complaining about such a construct strikes me as a bit odd.
- RealityVoid 10y agoWell, hello there, fellow MISRA warning wrangler! I think you're right and I usually find MISRA gives decent warnings and solving the warnings is quite instructive. I remember, for example, John Carmack had nice things to say about static code analysis.
- 10y ago
- brandmeyer 10y agoAdd -Wdouble-promotion to the list. For microcontrollers which implement float in hardware and double in software, it can be a lifesaver.
- revelation 10y agoThat's a great hint, I was almost at the point of writing a short script that checks the binary output for any soft FP operations.
- fest 10y agoYes, that's a very good idea, especially if code uses lot of floating point operations.