How smart C compiler is?

7 posts / 0 new
Last post
jap
jap's picture
Offline
Last seen: 10 months 2 weeks ago
Joined: 2011-06-04 05:53
How smart C compiler is?

Hello,

I've always assumed that a C compiler (like gcc) compiles all functions, including those that you never call. Is it so?

I'm thinking of a situation where I have a .h file containing three independent functions: funcA(), funcB(), and funcC(). In the main program I call the functions funcA() and funcB() but never the function funcC().

Is the compiler smart enough to not compile the unused function, i.e. leave it out from the object file, or do I have to use #ifdef and #define to exclude the unused function?

Rigo
Rigo's picture
Offline
Last seen: 2 years 6 months ago
Joined: 2011-01-24 21:55
Re: How smart C compiler is?

Things like that usually depend on your optimisation settings. check out the -o parameter for gcc for more information.

Simon

hypex
hypex's picture
Offline
Last seen: 4 months 2 weeks ago
Joined: 2011-09-09 16:20
Re: How smart C compiler is?

Rigo isn't that the output switch? I think you mean O. ;-)

I was almost caught by that last night before I checked the docs.

For me it seems a bad idea to have different functions for lower or upper options. Sure to a computer it's another number. But humans are inputting the information and it can look the same or similar enough before you discern the difference.

tboeckel
tboeckel's picture
Offline
Last seen: 4 years 6 months ago
Joined: 2011-09-13 12:32
Re: How smart C compiler is?

If all three functions are contained in a single .c file then the linker has no choice. It must include all of them if any of them is referenced.

Better put all three functions into distinct .c files. Then the linker is able to exclude those .o files which contain functions that are not referenced from anywhere.

jap
jap's picture
Offline
Last seen: 10 months 2 weeks ago
Joined: 2011-06-04 05:53
Re: How smart C compiler is?

I glanced through the gcc manual and it seems that the gcc compiler will always include all unused functions unless you do what tboeckel suggests or you exclude unused functions by using #ifdefs.

What I found was that the gcc has an option (-Wunused-function) which makes the compiler warn of unused static functions. So it's possible to get warnings on some unused functions but not all.

thellier
thellier's picture
Offline
Last seen: 5 years 12 months ago
Joined: 2014-03-04 12:43
Re: How smart C compiler is?

hello
Perhaps you can do that using inlining
I explain for inlined functions gcc got some option (name?) for not keepin a copy of (inlined) function but only to inline it where it is used

Alain Thellier

Alain Thellier - Wazp3D

corto
corto's picture
Offline
Last seen: 7 years 10 months ago
Joined: 2011-09-09 11:43
Re: How smart C compiler is?

I've just tested and whatever the optimization level, an unused global function won't be removed.

The option -Wuused-function will only warn for unused static functions. Note that a Linux tool like cppcheck should help to find unused global functions (it works parsing AmigaOS code).

For what you are looking for, the option -flto (link time optimization) will remove unused functions, but the compilation / link will be slower and you won't be informed about these unused functions.

Log in or register to post comments