Which gcc should i use
On my macosx system gcc That way I can pretty easily try out things, update gcc-HEAD to have the bleeding edge, but continue to have working and supported versions for my day to day development. Nothing is more annoying than having a compiler change slightly in annoying ways missing defines, etc This is even more important for embedded development, so far that I actually save the compiler toolchain into git.
A slight bump in version in gcc could mean either a horribly annoying compiler bug those happen much more often on embedded platforms I have the impression , or a bump in size of for example 40 bytes, which could completely obliterate your project.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Asked 9 years, 10 months ago. Active 9 years, 10 months ago. Viewed 7k times. EDIT: By safe - i mean mainly bug free, i. Does latest mean HEAD or latest release 4. Should we assume that by safe you mean safe execution in production? I edited to clarify what i mean in terms of safe.
By HEAD - what do you mean? Could you clarify? Optimizing compilation takes somewhat more time, and a lot more memory for a large function. With -O , the compiler tries to reduce code size and execution time, without performing any optimizations that take a great deal of compilation time.
GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. As compared to -O , this option increases both compilation time and the performance of the generated code. This is often the best option to use. This is the default. It also performs further optimizations designed to reduce code size.
It also enables optimizations that are not valid for all standard compliant programs. It should be the optimization level of choice for the standard edit-compile-debug cycle, offering a reasonable level of optimization while maintaining fast compilation and a good debugging experience.
We will use -Og in CS when we are debugging. This document and its content are copyright Stanford University, This can lead to some confusing side effects if the programmer thinks unique instances of the variable are being operated on within each c file.
A neat way to catch this kind of issue is to compile with -fno-common which disables the ability for tentative definitions to be merged into a pre-existing definition. This will lead to a duplicate symbol warning being emitted at link time:.
For embedded development, there is often not a lot of RAM available to allocate for stack space typically on the order of a few hundred to a few thousand bytes. Therefore, stack overflows can be very common especially if the programmer is not careful about what local variables they put on the stack.
To generate stack depth information, you need to use the -fstack-usage flag which will emit. Tools like Puncover 8 will even analyze the stack space information emitted and generate maximal stack depth estimations based on call path. We will see the stack usage of the function in bytes followed by static if the usage is fixed or dynamic if the entire stack usage cannot be computed at compilation time because a variable length array VLA was used. When arithmetic operations are performed on integer types i.
First integer promotion occurs. The C standard defines this as 9 :. If an int can represent all values of the original type as restricted by the width, for a bit-field , the value is converted to an int ; otherwise, it is converted to an unsigned int. It can be hard to keep track of the set of conversions taking place but fortunately the -Wconversion option can be used to generate warnings when implicit conversions that are likely to change the underlying value take place.
When a binary is loaded on an embedded device, it is important that debug information is included in the ELF. Otherwise, gdb or your IDE will not be able to display variables or backtraces. Typically you will see a project use -g but consider compiling with -g3. It will include a couple extra goodies such as macro definitions used in your application which can be useful to be able to print in gdb especially when the definition comes from a chain of definitions.
It enables a good balance of flags which optimize for size as well as speed. Forgetting to flip on this flag can have serious code size impacts. For more details check out the interrupt series of posts about it! This becomes very clear when examining the mapfile and seeing a bunch of symbols in the.
With -ffunction-sections , each function gets its own section:. As the linker resolves references between objects it will mark a section as used. A final optimization pass it can make is to cull any unused sections. For larger projects, this will often result in substantial code savings.
This optimization is enabled by passing --gc-sections to the linker. Diagnostic logs about the sections dropped can also be displayed using --print-gc-sections linker option. It can be quite an exuberant warning in existing codebases. A really unfortunate flag that is hard to avoid is -fshort-enum. This flag allows the compiler to change the type of an enum based on the declared range of values.
When an enum is trucated to a smaller width, the same type of masking instructions may be needed when operations are performed with it. The arm toolchain by default is compiled with -fshort-enum so if you are using standard libraries such as newlib libc.
You can disable short enums by setting the -fno-short-enum flag but if you try to link against a unit that enables short enums such as newlib you will see the following linker warning:. Due to the nuances of enums in C, never use enums directly in packed structures. Instead use a type with a constant size and assert if the enum does not fit within the allocated size. LTO link time optimization , is an optimization pass made across all compilation units, that can help reduce the overall size of a binary.
0コメント