c/c++ header files

jj Sterling Heights, MI Icrontian
edited July 2007 in Science & Tech
At work there is a debate on how to use header files (.h files). Some say that .h files should only have function prototypes and definitions in the file. I use them a bit differently and I say it's perfectly professional and legal to do so. I usually put the actual function and the prototype in the .h file. My co-works say that the actual functions should be a .c file and include a .h file with the prototypes with in the .c file. If I'm using the #include what difference does it make if I include a .c or .h. As I understand it all the #include does is paste what ever is in the .h or .c inline at compile time. I want to know other peoples thoughts about placing the function in a .h file.

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited July 2007
    If i understand your question correctly, it's probably better to just have the .h file with the prototypes. If you're including the .h file with the source for all the functions, then you've got copies of the entire source in every .c file. If you only have the prototypes in the included .h file, then you're better off, i think.

    The other advantage is that in general, people associate the .c file with all the source, and the .h files with only the prototypes, so they know where to find everything.

    I agree with your co-workers. .c is for source, .h is for the prototypes and etc.

    The linker is designed to do exactly what your code is doing
  • CBCB Ƹ̵̡Ӝ̵̨̄Ʒ Der Millionendorf- Icrontian
    edited July 2007
    Nah, If it does what it's supposed to do in the end, then it's right.
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited July 2007
    won't the binaries be bigger?
  • JBJB Carlsbad, CA
    edited July 2007
    j wrote:
    If I'm using the #include what difference does it make if I include a .c or .h. As I understand it all the #include does is paste what ever is in the .h or .c inline at compile time.

    You should only be #including the .h file in other files that need to use your type. If you have ClassA.h and ClassA.cpp you should put the declaration in the .h file and the implementation in the .cpp file. If ClassB then needs knowledge of ClassA you simply #include ClassA.h. This way the implementation of ClassA (ClassA.cpp) is built once during compile time and liked appropriately.

    Actually, the more I think about this I'm surprised your code will compile this way. Are you working with namespaces? How does the compiler handle the re-definition of your functions? If you put your implementation in ClassA.h, how many times do you #include ClassA.h in your project?

    See Problem #4: http://www.gamedev.net/reference/programming/features/orgfiles/page4.asp
  • jj Sterling Heights, MI Icrontian
    edited July 2007
    Radeon_Man wrote:
    Actually, the more I think about this I'm surprised your code will compile this way. Are you working with namespaces? How does the compiler handle the re-definition of your functions? If you put your implementation in ClassA.h, how many times do you #include ClassA.h in your project?

    See Problem #4: http://www.gamedev.net/reference/programming/features/orgfiles/page4.asp

    Actually I'm using C, not C++. I agree if I was using C++ I might be thinking differently. My programing is application specific. Where I might use a function from another program 1 or 2 times at the most. I don't run into a situation were I use all the same functions on a project then I do on another. If I wanted to create a modular program then yes I would use the header files for prototypes and organize things so they can be used in other places. I program micro controller and for 8 bit controllers the code just isn't at a point were I NEED to add the complexity of modular programing. Copy and pasting functions works really well. However Shwaip made a good point. People are expecting the .c files to hold functions. I think maybe I'll just name the file swith functions a .c. Really it doesn't matter because all my variables are global anyway. I do this because the programs runs faster that way (on a micro controller). Passing variables back and forth takes clock cycles. So I think to sum it up. Yes, you can put functions in the .h (and it's prob easier to read that way) but, since the "Norm" is the have .c's I will switch to that. That should make some people happy and it makes no difference to me. I should just switch to Java and not deal with header files. :-)
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited July 2007
    hahaha...java on a microcontroller. hahaha

    Yeah...it's important to keep coding conventions consistent across all developers on a project. It'll make them happy, and if someone completely different has to go back and edit them later, they won't be like WTF?
Sign In or Register to comment.