2

I am new, so I appreciate your help and patience in advance. I have written a program in C like this:

main.c
arpsend.h - header w/include guard for arpsend.c functions
arpsend.c - includes <net/if.h>, <pthread.h>, etc.
arprec.h  - header w/include guard for arprec.c functions
arprec.c  - includes <net/if.h>, <pthread.h>, etc.

The arpsend and arprec files use many of the same kernel library functions and definitions. I have noticed that my program size shot up when I wrote the arprec.c code. It bloated much more than what my code should have. This leads me to conclude that both the arpsend.c and the arprec.c linked the linux library code necessary for their own needs in their respective .c files. The linking is redundant for the project, but necessary for each .c file.

My questions are the following:

  1. if every .c file I add to a project will bloat like this because of kernel and standard library redundancies, wouldn't every program become needlessly bloated? The bloat in my example is probably insignificant (~12k), but I can only imagine the kind of bloat that would happen if I needed to use some graphics library across several different .c files.

  2. Is there a way to avoid this?

  3. Is the recommended solution to simply keep all functions using the same kernel code in one file?

  4. If #3 is correct, doesn't that defeat the point of trying to keep clean code? It's C, so it's not really OOP, but I would like to spread my code out so that I can easily see the makeup of a project.

I apologize if this is redundant. I sifted through the forums here for a couple hours. I couldn't find my exact question. Thanks again for your help

  • Welcome to Stack Overflow. Please read the [About] page soon. How are you measuring size? What was the size of the program before you wrote `arprec.c`? What was the size afterwards? What is the size of `arprec.o` and `arpsend.o`? Is your code being built into the kernel, or is it a regular user program that is using system headers? Are you using kernel headers or system headers, even? – Jonathan Leffler Jul 28 '13 at 02:39
  • Have you looked to see if those other files contain include guards ? – johnish Jul 28 '13 at 02:39

2 Answers2

2

How much do you think your binary size should have increased when you added the arprec.c file to the project, as opposed to how much it did increase?

The library functions are only linked once into the final executable, regardless of how many files in the project use them. The linker is usually smart and only includes the library functions actually used by your code in the final executable; is it possible that arprec.c uses library calls that arpsend.c does not?

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • Oh! I didn't know people had posted to this. Thanks to all for responding. Again, I'm fairly new to this, so I appreciate your help. People who go out of their way are awesome. – user2626754 Aug 07 '13 at 20:09
  • No, I don't think it's possible that arprec.c uses any calls that arpsend.c does not. In fact, arprec.c is just a function that I moved from arpsend.c into its own file. I had to copy all of the includes, of course. But the move alone is what added another 12k in size after compilation. – user2626754 Aug 07 '13 at 20:26
0
  1. Only needed function are linked. It does not matter on how many files you will split your program.

  2. If you use shared libraries, linking will be done at run time.

  3. No.

Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53