Frage

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

War es hilfreich?

Lösung 2

  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.

Andere Tipps

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?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top