Why gcc gives error of unused variable for local variables but not for global variables? - gcc

I have a question regarding gcc. Why I get an error of unused variable when I define the variable locally in a function but not when the variable is global in a unique file?.
I can understand that it can be use for someone else, but to do that then I need to put the external word right?
Thanks in advance.

The compiler has no way of knowing if a global variable is used - it could be used from a compilation unit written in a completely different language, for example.

If by "global in a unique file", you mean "int x;" outside of any function, the it's not the compilers job to detect that, the variable needs to be available to the linker in case another compilation unit needs it (such as errno).
If you meant "static int x" where it's not made available to the linker, this is probably just a choice made by GCC. I don't believe compilers are required to notify of this and it does no real damage other than wasting a few bytes in your address space.

Because global variables can be used on any other place that the compiler cannot known. For instance on a external library o program.

Unused locals can be determined by the compiler. Unused globals can only be determined by the linker, since they can be shared across object files.
In general, the linker doesn't do warnings for code-gen.

When the variable is global, the compiler has not full visibility across all the compilation units in the project - the variable could be modified in another compilation unit. The linker is able to tell that it is unused, probably it will remove it from the object file.

Because if it's global it can be used by another module that gets linked in later.
It's a common idiom to have all your globals defined in a single file. That file may not even have any code, much less code that uses all the variables.

I have encountered the same question when I build the dalvikVM in android2.3 and I got the key of the point. It is because that the parameters of the compiler is too strict:
LOCAL_CFLAGS += -Werror.

Related

Can the gnat compiler find unused specification procedures/functions/variables?

Is there a warning option switch that will identify spec-level procedures, functions, or variables that are not called or referenced anywhere? I've tried the switches below without luck.
This is what I'm currently using:
-gnatwfilmopuvz
-- m turn on warnings for variable assigned but not read
-- u turn on warnings for unused entity
-- v turn on warnings for unassigned variable
When I move unused variables from the spec to the body, the compiler correctly identifies them as not referenced. I would like to understand why the compiler won't identify unused code in the spec, and if there is a way to get it to do so. An excessive number of warnings isn't a concern, because I use the filter field in gnat studio to only look at a few files at a time, and I can easily filter to ignore library packages.
Any help is very appreciated.
The compiler will only detect unused items in the unit it is compiling.
If you have items in a package spec, you can know they are used (or not) only by exploring the whole project's Ada sources. Some tools like AdaControl can do it.
You need a tool for that: gnatelim. Its main use is to reduce the size of the executable, eliminating the object code for unused subprograms, but you can use its output just to get the list of unused subprograms. As far as I know, it will not detect unused variables in the spec, only procedures and functions.
https://gcc.gnu.org/onlinedocs/gcc-4.5.4/gnat_ugn_unw/About-gnatelim.html
Use link-time garbage collection: https://docs.adacore.com/live/wave/gnat_ugn/html/gnat_ugn/gnat_ugn/gnat_and_program_execution.html#reducing-size-of-executables-with-unused-subprogram-data-elimination
You can then add the linker option --print-gc-sections to instruct the linker to print out a list of all symbols that were garbage collected.

Is there a way to prohibit the declarations of global variables in C/C++ when using GCC?

Say, if a project leader don't want his team members to use global variables, how can he do this technically? Is there an option when building GCC that makes it throw errors when it knows someone has declared a global variable?
And static global is allowed, however.
As requested
How do you plan to use stdin, stdout, stderr and errno (to name but four global variables) if you can't declare global variables? Do you really mean you don't want people to define global variables — though library implementers are exempted because they're required to defined stdout etc?
My suspicion is that you'd do best parsing the output of nm -g file.o (or thereabouts; the options can vary between systems, and the output formats vary dramatically between systems), looking for global variable definitions. You could look for global variable references, but system libraries use them too, so you'd have to know which global variable references are OK and which are not.
I'm not aware of a GCC option for the job, but that doesn't mean it doesn't exist. Have you looked at the manual? I did look and didn't find an appropriate warning — probably because it isn't a common requirement.

Does specifying parameters or variables as __attribute__ ((unused)) allow the compiler to perform any additional optimizations?

I'm particularly curious about LLVM 4.1, but would be interested in other compilers' behavior as well.
According to the GCC documentation (which LLVM supports at least in part), the unused attribute has the following behavior:
This attribute, attached to a variable, means that the variable is meant to be possibly unused. GCC will not produce a warning for this variable.
If the compiler is able to warn you about unused parameters and variables, though, presumably it already knows what parameters and variables are unused without you having to tell it (especially since the unused attribute only indicates that the variable is possibly unused). Therefore, does the unused attribute allow the compiler to perform any additional optimizations, or is its purpose just to allow for more readable code? Also, if the unused attribute does in fact allow the compiler to perform additional optimizations, what happens if you actually end up using a parameter or variable that was specified as unused? LLVM (in XCode) did not seem to complain about this case, though it's possible I wasn't compiling at the right optimization level or with the right warnings enabled.
__attribute__((unused)) doesn't help optimization, and it doesn't mean that the value is necessarily unused. It suppresses warning (if there is a reason for this warning, that is, if the value is indeed unused), that's all.

"irq_to_desc" undefined?

everybody.
I need to use $irq_to_desc in my project, but despite the fact I included all h files it needs, gcc still emits ""irq_to_desc" undefined!" messages. I found something on the topic here http://comments.gmane.org/gmane.linux.kernel.kernelnewbies/34403 but I still dont understand how to fix this prroblem.
I don't believe you can use irq_to_desc() in a module.
If CONFIG_GENERIC_HARDIRQS isn't defined, then irq_to_desc() is #defined as a macro in include/linux/irqnr.h. Since the variable it references, irq_desc, isn't in an EXPORT_SYMBOL or EXPORT_SYMBOL_GPL declaration, I don't think you could link a module using that variable into the kernel -- only statically compiled in-kernel code can use it.
If CONFIG_GENERIC_HARDIRQS is defined, then a function irq_to_desc() is declared in include/linux/irqnr.h and defined in kernel/irq/irqdesc.c. There are two definitions of irq_to_desc() in kernel/irq/irqdesc.c depending upon the value of CONFIG_SPARSE_IRQ. There is no corresponding EXPORT_SYMBOL or EXPORT_SYMBOL_GPL declaration for the function, so it can't be used in modules -- only statically compiled in-kernel code.

Is there a way to strip all functions from an object file that I am not using?

I am trying to save space in my executable and I noticed that several functions are being added into my object files, even though I never call them (the code is from a library).
Is there a way to tell gcc to remove these functions automatically or do I need to remove them manually?
If you are compiling into object files (not executables), then a compiler will never remove any non-static functions, since it's always possible you will link the object file against another object file that will call that function. So your first step should be declaring as many functions as possible static.
Secondly, the only way for a compiler to remove any unused functions would be to statically link your executable. In that case, there is at least the possibility that a program might come along and figure out what functions are used and which ones are not used.
The catch is, I don't believe that gcc actually does this type of cross-module optimization. Your best bet is the -Os flag to optimize for code size, but even then, if you have an object file abc.o which has some unused non-static functions and you link statically against some executable def.exe, I don't believe that gcc will go and strip out the code for the unused functions.
If you truly desperately need this to be done, I think you might have to actually #include the files together so that after the preprocessor pass, it results in a single .c file being compiled. With gcc compiling a single monstrous jumbo source file, you stand the best chance of unused functions being eliminated.
Have you looked into calling gcc with -Os (optimize for size.) I'm not sure if it strips unreached code, but it would be simple enough to test. You could also, after getting your executable back, 'strip' it. I'm sure there's a gcc command-line arg to do the same thing - is it --dead_strip?
In addition to -Os to optimize for size, this link may be of help.
Since I asked this question, GCC 4.5 was released which includes an option to combine all files so it looks like it is just 1 gigantic source file. Using that option, it is possible to easily strip out the unused functions.
More details here
IIRC the linker by default does what you want ins some specific cases. The short of it is that library files contain a bunch of object files and only referenced files are linked in. If you can figure out how to get GCC to emit each function into it's own object file and then build this into a library you should get what you are looking.
I only know of one compiler that can actually do this: here (look at the -lib flag)

Resources