If you use #ifdef/#endif, the part in between isn't compiled at all. The 
preprocessor will not forward the source to the compiler and the compiler never 
sees it.
I use it often to customize parts of a function depending on the used processor 
and/or architecture. So I can conpile a function for ATMega and several MSPs 
and depending on the current project, some code is 
different or left out.

Using if(0) (or while(0)), however, generates unreachable code which is 
eliminated by the comiler optimization run. It is compiled, detected as never 
executed and discarded. And also not part of the compiled code and 
therefore never linked.

The suggested compiler/linker flags, however, let the compiler treat each 
function as a separate compilation unit, starting at its own 0-offset address. 
This means teh compiler will not resolve any local references and 
let it all to the linker. Since all references are passed to the linker, the 
linker can link or discard every of these sections depending on the open 
references. What's references from a different module is kept, what's not 
referenced will be discarded. But it may happen that parts are discarded 
bwcause not yet referenced by any of the already loaded modules, bu twill be 
later and then the reference cannot be resolved as the target had 
been discarded already. Also, it keeps the compiler for doing optimizations 
like common subexpression elimination or using the most efficient way to call 
functions or access variables, as they are all referenced as 
externals.

If you want it or not depends on your project.

JMGross.

----- Ursprüngliche Nachricht -----
Von: Grant Edwards
An: [email protected]
Gesendet am: 15 Jun 2010 17:54:00
Betreff: Re: [Mspgcc-users] Unused functions occupying unnecessary memory

On 2010-06-15, Alex Polbach <[email protected]> wrote:

> Thanks a lot for so fast answers!!
>
> I have added both flags to CFLAGS and LDFLAGS and they work just fine.
> Very neat solution.

I'm not sure how much those features get used with mspgcc, but I found
them to be invaluable if you want to share code between projects or if
you want to be able to do different builds of a project that have
different features enabled: when you disable (e.g. with #if/#endif) a
function, then all of the code/data that were used only by that
function is automatically left out.  IIRC it worked even when you
disable code with if(0) {}.

When I first implemented those features for mspgcc there were obscure
cases where a function would get discarded when it shouldn't have been
due to omissions in the linker script.  AFAIK, that shouldn't be a
problem, but it's something to keep it in mind if you get linker
errors due to inexplicably missing symbols: the solution is to mark
the symbol with the KEEP() command in the linker script.

-- 
Grant Edwards               grant.b.edwards        Yow! Will it improve my
                                  at               CASH FLOW?
                              gmail.com            


------------------------------------------------------------------------------
ThinkGeek and WIRED's GeekDad team up for the Ultimate 
GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the 
lucky parental unit.  See the prize list and enter to win: 
http://p.sf.net/sfu/thinkgeek-promo
_______________________________________________
Mspgcc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users


Reply via email to