C Functions

Go To Last Post
10 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is my first post for 2012, so happy new year AvrFreaks!!

I have some questions regarding c functions. I noticed in the lss file that uncalled inline functions do not compile, while uncalled normal functions do. I know that uncalled functions should not exist in the source code in the first place, but i am just curious. Shouldn't the compiler do the same thing in both cases? Is there a way to tell gcc to check for uncalled functions and not compile them?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Shouldn't the compiler do the same thing in both cases?
No. C files are compiled individually, so the compiler does not know whether or not a regular function is called from some other C file. Therefore the compiler can not remove the function unless it knows that it absolutely can not be called from some other file (such as when the function is declared static inline).

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
C files are compiled individually
I compiled a single file. Are you saying that the compiler does not know that?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Correct. How can it know?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is the linker that determines what functions are called from outside the compile unit, not the compiler.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I compiled a single file. Are you saying that the compiler does not know that?

Think about this: What if I agreed to deliver some code to you, but I do not want to show the source code. So now I compile my source and only send the object file to you, which you use in your projects.

How will my compiler know if you are going to call a certain function that I wrote?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

(gcc can actually omit unused functions at the linker stage. Arduino uses this to keep from being completely bloated, for instance. The relevant options are "-ffunction-sections" during compile and "-Wl,--gc-sections" during link.)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It make sense now. Thank you all for your input.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

> I compiled a single file.

Declare your function "static", then the compiler knows there's no
chance the linker could ever refer to the unused functions, and it
can omit them as well.

If you're too lazy for that, use -fwhole-program, and the compiler
will treat all functions as if they had been declared "static".

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Declare your function "static", then the compiler knows there's no chance the linker could ever refer to the unused functions, and it can omit them as well.

If you're too lazy for that, use -fwhole-program, and the compiler will treat all functions as if they had been declared "static".

The single file was just a test. Initially i noticed the compiled unused functions when i included some other c files in my project, but that make sense because, as explained to me in previous posts, the compilers compiles c files individually and the linker links all o files, so i made the test with just a single file to see if the result will be the same. I understand the concept of static functions, but that was not the issue in the first place. I apologize if i misled you.

I think i'll try what westfw suggested to see if i get the desired effect.