Thread: Is there a practical reason to NOT have a single header file for eveyr public symbol?

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    138

    Is there a practical reason to NOT have a single header file for eveyr public symbol?

    Actually the title says it all. From what I've noticed, most symbols in a file will be private ("static" to use C terms) to this file so why not have just 1 header file for every symbol in our program.

    Are there problems with these approach and any practical reasons other than things like: "this is how we learnt to do", "it's a good practice" etc.?

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    The most common reason is you don't want all code to see those symbols, for example you may be mapping system APIs or variables to common symbols but also want to reduce the chance they could be corrupted by rogue or poor quality code, an example from one of my own projects is main_thread, I need it for synchronising spawned threads but don't want some external code trying to fiddle with it in ways it should, therefore both the contents of main_thread and the symbol itself are only declared inside the code that becomes part of the shared libarry I build, external code only gets to see the pointer type, anything else is completely hidden.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    945
    If for no other reason, it would be unwieldy to have one header file for each symbol for anything but the most trivial programs. A common thing to do is to have one header file per "module" (which may be roughly analogous to a class in C++).

    Imagine you have an LCD "module" for printing stuff to an LCD screen. You might have 15 different functions (each function is a symbol) to clear the screen, move the cursor, print a string, write a character, create custom characters, etc. Would you really want to have 15 header files, one for each exported function (which you would have to include in every source file that uses the LCD "module")? Or would you rather have a single header file that describes the interface to the LCD "module"?

  4. #4
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by christop View Post
    If for no other reason, it would be unwieldy to have one header file for each symbol for anything but the most trivial programs. A common thing to do is to have one header file per "module" (which may be roughly analogous to a class in C++).

    Imagine you have an LCD "module" for printing stuff to an LCD screen. You might have 15 different functions (each function is a symbol) to clear the screen, move the cursor, print a string, write a character, create custom characters, etc. Would you really want to have 15 header files, one for each exported function (which you would have to include in every source file that uses the LCD "module")? Or would you rather have a single header file that describes the interface to the LCD "module"?
    Oh! Yeah, I totally understand what you are talking about in it makes a lot of sense! This is how I will think of it from now on! Thanks a lot!

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    138
    Quote Originally Posted by awsdert View Post
    The most common reason is you don't want all code to see those symbols, for example you may be mapping system APIs or variables to common symbols but also want to reduce the chance they could be corrupted by rogue or poor quality code, an example from one of my own projects is main_thread, I need it for synchronising spawned threads but don't want some external code trying to fiddle with it in ways it should, therefore both the contents of main_thread and the symbol itself are only declared inside the code that becomes part of the shared libarry I build, external code only gets to see the pointer type, anything else is completely hidden.
    That's sure interesting! I think in the case the best way is, keep what you don't care in the same, single file and keep stuff that should not be public to anything in another file (like the "main_thread" you described) right?

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by rempas View Post
    That's sure interesting! I think in the case the best way is, keep what you don't care in the same, single file and keep stuff that should not be public to anything in another file (like the "main_thread" you described) right?
    yep

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reason behind multiple levels of header files?
    By Leam in forum C Programming
    Replies: 6
    Last Post: 01-24-2016, 10:10 AM
  2. Replies: 11
    Last Post: 04-18-2011, 05:44 PM
  3. Replies: 6
    Last Post: 08-26-2008, 12:38 PM
  4. Replies: 30
    Last Post: 06-19-2006, 12:35 AM
  5. Replies: 4
    Last Post: 12-14-2005, 02:21 PM

Tags for this Thread