Solved

use of variable attribute

Posted on 2013-12-02
4
234 Views
Last Modified: 2013-12-03
I saw at some places in my code the functions were defined in this way

#define USED_ATTR __attribute__ ((used))
return_type  func_name(parameters) const  USED_ATTR  { }

I tried to read this but cannot understand the real use case of this
http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348c/BABCJJID.html

What exactly is    __attribute__ ((used))  doing underneath
What does it mean by retain in object file
0
Comment
Question by:perlperl
  • 2
4 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 300 total points
ID: 39689843
Hi perlperl,

at http://www.keil.com/support/man/docs/armccref/armccref_BABCIGHE.htm you I found this:
Functions marked with __attribute__((used)) are tagged in the object file to avoid removal by linker unused section removal.
Further at http://www.keil.com/support/man/docs/ARMLINK/ARMLINK_Cchhhghb.htm you can find this:
This causes armcc to generate the symbol __tagsym$$used for each of the functions and variables, and ensures that the function or variable is not removed by the linker.
So this means the functions tagged with this attribute aren't removed from the binary by the linkter even if they are not referenced (i.o.w. used) anywhere in the code. I guess this is mostly useful when implementing libraries, but I'm not sure about this, I have no experience with ARM as target platform.

Hope this helps,

ZOPPO
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 39689852
>>What does it mean by retain in object file

The original quote is

This variable attribute informs the compiler that a static variable is to be retained in the object file, even if it is unreferenced.

Usually, the compiler would remove symbols (i.e. variables or functions) that aren't used and marked as 'local' ('static') to save space and avoid redundancy (whether that happens when explicitly not optimizing is up to the compiler, but seems to be the case here). So, by explicitly marking them as 'used', the compiler is instructed to leave them in the created module, regardless if they are references (== used) ur not.
0
 

Author Comment

by:perlperl
ID: 39689967
so its strictly for optimization/performance use case and nothing to do with the symbol defined elsewhere in the library?
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 200 total points
ID: 39689972
Such as the docs suggest: Yes. Yet it's not 'optimization', I'd call the category 'Code Generation', since it manipulates the way the compiler creates the output.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 4 184
How to install SVN Command Line Client? 5 99
C++ help/ Toy problem 19 49
Coding for the first time 9 62
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question