Thread: #include causes missing symbol definitions

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    #include causes missing symbol definitions

    If you include one .h file, should that ever force you to include another .h file as well? Or would that never happen in good design?

    For example. Suppose you have a compiler error:

    Code:
    myfile.h: error: 'T_UINT32' undeclared (first use in this function)
    So you look up where T_UINT32 is defined and you add the following to the top of your file:

    Code:
    #include "mytypes.h"
    Then when you try to compile you get another error:

    Code:
    In file included from myfile.h
                     from mytypes.h:h:252: error: expected specifier-qualifier-list before 'uchar'
    All .h files have proper header guards in them. I am running into a situation like this and it does not make sense to me. Does this imply an error in a .h file somewhere?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What kinds of specifiers/qualifiers does myfile.h have for uchar?

    It should be obvious reading through myfile.h, yes? I wouldn't focus on the "mistake", if there is one. Just do the fix and remember what it is.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    It's not that I can't fix this bug, but more that I am asking if including a single .h file will ever make you include others as well, or if this is bad design.

    As for the message "expected specifier-qualifier-list before 'uchar'" it's a typical compiler error which is totally misleading. There's absolutely nothing wrong with the line in question (it's a line defining a member in a structure), except for the fact that without including some other files the type 'uchar' is undefined since it is not one of C's built in types.

    Basically the symptom is after I include myfile.h the compiler complains and I also have to include a separate file to make mytypes.h happy. I would have expected that any decent header file should include all of the required function definitions, types, symbols etc itself.

    Another way to think of it is like this:

    file A requires file B so it includes file B
    file B requires files C, D and E, but it only includes files C & D
    In file A you want to use some stuff from file B but you must include both file B and file E

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mjl3434 View Post
    It's not that I can't fix this bug, but more that I am asking if including a single .h file will ever make you include others as well, or if this is bad design.
    In a relatively simple system, yes I would judge that to be bad design.

    But as system complexity increases that judgement becomes less clear. Windows for example has 300+ include files none of which are worth a camel's butt without Windows.h, which also includes about a dozen other headers of it's own accord. On a system that complex interdependency is almost unavoidable as you absolutely don't want to include all those headers into something puny as "hello world"...

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mjl3434 View Post
    It's not that I can't fix this bug, but more that I am asking if including a single .h file will ever make you include others as well, or if this is bad design.
    It's bad, but it's even worse when there's nothing telling me which headers I need to include to get it to work properly. In the worst case, I might be able to build something with a wrong set of headers and not know that I've done that.

    If A.h needs some type defined by B.h then A.h should include B.h, that simple. If the headers start to become inefficient then you can start optimizing some of them by forward-declaring types instead of including other headers, but that's done purely within the headers. No .c or .cpp code file should ever need to "guess" which headers are needed to support some other header.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 12:47 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. <list>
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 04:07 PM