Developer forums (C::B DEVELOPMENT STRICTLY!) > Development

How to exclude unused functions

<< < (2/4) > >>

oBFusCATed:

--- Quote from: joneewheelock on November 20, 2016, 09:04:41 am ---The options -ffunction-sections -fdata-sections are for linker right?

--- End quote ---
They are only compiler options. It tells the compiler to put every function in a separate section, this means that if the linker finds that a section is not used it can remove it. I don't remember if some linking option should be passed to tell the linker to do this optimization. But this is easily testable - add a function you don't use and check the log if the sizes stay the same then the optimization is working.

joneewheelock:
Thanks for good response.

--- Quote from: BlueHazzard on November 20, 2016, 01:15:51 pm ---well according your example project, there is only a main function. I don't know what the compiler should optimize there...

--- End quote ---
Sorry that I did not give the complete code. I gave only project folder with main just to give the project settings to you people. Below is the main code (compilable, but does not do anything). Even if f1() is commented or uncommented, the final size remains the same except 4 byte variation for function call itself  (182 or 186 bytes) .


--- Quote ---The linker removes the excessive function, not the compiler. So they are still present in the object file!!! But not in the binary file.
--- End quote ---
Sorry, could you pl. elaborate this point? I am not clear. Did you mean to say the size 182 bytes displayed is not that of binary file to be written to the controller? When I burn the file, the same 182 byte is displayed as the size written to flash.


--- Quote ---Because according your build log, you don't ask the compiler to generate a ls file
--- End quote ---

I added avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).ls.
But the listing is getting displayed in build log itself (Causing inconvenience because log will be huge) and no .lss file is getting created. I see a warning message at the end saying "Warning: '>' is not an ordinary file". Also note that while creating new project, I had opted for creating list file. But CB is not generating automatically.


--- Code: ---#include <avr/io.h>
#include <util/delay.h>
uint8_t a =12;
void f1 (void);
int main(void)
{


    while (1)
    {
        PORTB=a;
       //f1();

    }

}
void f1 (void)
{
    DDRD=0;
    DDRC=0xff;
    DDRD=0xff;
    uint8_t m=0x50;
    if(m>12)
    {
        PORTD=12;
        PORTC=16;
    }
}
--- End code ---

BlueHazzard:

--- Quote from: joneewheelock on November 20, 2016, 05:03:18 pm ---Sorry that I did not give the complete code. I gave only project folder with main just to give the project settings to you people. Below is the main code (compilable, but does not do anything). Even if f1() is commented or uncommented, the final size remains the same except 4 byte variation for function call itself  (182 or 186 bytes) .

--- End quote ---
yea, the output of avr-size is the amount of bytes that could be written on the controller


--- Quote from: joneewheelock on November 20, 2016, 05:03:18 pm ---I added avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).ls.But the listing is getting displayed in build log itself (Causing inconvenience because log will be huge) and no .lss file is getting created. I see a warning message at the end saying "Warning: '>' is not an ordinary file". Also note that while creating new project, I had opted for creating list file. But CB is not generating automatically.
--- End quote ---
i think the problem with this is, that the redirect/stream operator ">" does not work in the pre/post build steps in codeblocks. You have to use the line

--- Code: ---cmd /c "avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).lss"
--- End code ---
or

--- Code: ---xterm -e "avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).lss"
--- End code ---
on linux (i did not test this, and obviously you need xterm installed)

i think you need to add

--- Code: ----ffunction-sections -fdata-sections
--- End code ---
to the compiler settings
and

--- Code: -----gc-sections
--- End code ---

or

--- Code: ----Wl,--gc-sections
--- End code ---
to the linker settings. I am not quite sure if codeblocks calls the linker directly

the first command:

--- Quote from: oBFusCATed on November 20, 2016, 01:53:02 pm ---
--- Quote from: joneewheelock on November 20, 2016, 09:04:41 am ---The options -ffunction-sections -fdata-sections are for linker right?

--- End quote ---
They are only compiler options. It tells the compiler to put every function in a separate section, this means that if the linker finds that a section is not used it can remove it.

--- End quote ---
So the functions are still present in the object file
then you call the linker with the second command and the linker will garbage collect your unused functions...

greetings

joneewheelock:

--- Quote --- From BlueHazzard

--- End quote ---

Perfect. Now everything seems to be OK after I added -Wl,--gc-sections to linker settings and also adding cmd /c "avr-objdump -h -S $(TARGET_OUTPUT_FILE) > $(TARGET_OUTPUT_DIR)$(TARGET_OUTPUT_BASENAME).lss" to post build.
Now codeblock is setup completely for AVR. Attached is the template if someone else need this. In case anyone finds any other issues, please let me know. I can correct and update the template.

For those who are not familiar with templates:
Where to copy the template?
Type %appdata% in start menu and then go to codeblocks folder. Copy the UserTemplates folder from the attached zip file file to this codeblocks location. Now you can go to New->From Template option and then create your new project.

BlueHazzard:

Beside from the garbage collection compiler switches Codeblocks has an embedded template (wizard script) for avr. Why are you not using this? From there i have the lss command line ;)

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version