Thread: -Wmissing-prototypes. Should every source file include its header file?

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    104

    -Wmissing-prototypes. Should every source file include its header file?

    While reading an online tutorial Makefile - OSDev Wiki I found a section mentioning quite a few compiler warnings I'd never seen before. I thought Wall Wextra and pedantic included all the warnings, the important ones at least, yet it seems that is not the case.

    One of the warning flags caused the compiler to complain about some sections of my code: warning: no previous prototype for "functionName". Some digging showed me the problem was I didn't include the header file with the prototype in the source file containing the function definition.

    I didn't do it because the source file does not require anything from the header. If it had any structure or macros, obviously I would have; but this is not the case this time.The only reason for the header's existence is to prototype the functions of the source file so they can be used elsewhere in my program.

    Is it expected that every source file with external functions should include its own header with the function prototypes? If so, is there a particular reason, I mean it seems unnecessary and rather wasteful.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes.

    It ensures that the header file and source file remain in sync.

    Otherwise you could edit one and things would still build, but blow up in mysterious ways at runtime.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    104
    Ah...
    There's nothing to break in my case, since the header only has prototypes. But I understand the reasoning.
    Well, perhaps I could change the function parameters, or the return type... yeah, it makes sense.
    Thank you XD

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to include a file from any location in a .c source file
    By techie_san778 in forum C Programming
    Replies: 2
    Last Post: 08-24-2015, 02:06 PM
  2. Replies: 2
    Last Post: 12-11-2014, 04:53 AM
  3. Does #include indirectly include the source file too?
    By Lord Asriel in forum C Programming
    Replies: 10
    Last Post: 11-30-2011, 08:20 AM
  4. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  5. Replies: 3
    Last Post: 02-10-2009, 02:51 PM

Tags for this Thread