Thread: using definition from header file without including it

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    12

    using definition from header file without including it

    Hi,
    Is there a way to use a definition from header file without doing #include to the whole header
    i have a big header which i can't remove definitions from it
    i need only 1 defnition in my c file, but i need to have it as small as possible
    so including the whole header is not the best way
    is there another way?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    The compiler should be smart enough to remove dead (unused) code, so you shouldn't worry about it. Try compiling with the whole header, and also with just the one definition included, and compare sizes to be sure.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You could technically just write out the declarations from the header file manually, but all in all, I wouldn't recommend that. Headers are written the way they are to include the functionality they need. To pick and choose what to keep sounds a little odd. Someone -- I believe it was brewbuck -- likend header files to menus at restaurants. You shouldn't really need to edit the menu or rewrite your own.

    Are you sure you know what you're doing? This doesn't make sense if you are going to statically link to a library that has the implementation of one function that you need.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    Quote Originally Posted by robatino View Post
    The compiler should be smart enough to remove dead (unused) code, so you shouldn't worry about it. Try compiling with the whole header, and also with just the one definition included, and compare sizes to be sure.
    Well.. the obj file was smaller when removing the include file but the exe file was the same.
    i wonder if its the same for sys file also...
    thanks

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    12
    Quote Originally Posted by MacGyver View Post
    You could technically just write out the declarations from the header file manually, but all in all, I wouldn't recommend that. Headers are written the way they are to include the functionality they need. To pick and choose what to keep sounds a little odd. Someone -- I believe it was brewbuck -- likend header files to menus at restaurants. You shouldn't really need to edit the menu or rewrite your own.

    Are you sure you know what you're doing? This doesn't make sense if you are going to statically link to a library that has the implementation of one function that you need.
    yes i'm sure ...
    i want to use one #define x y from a BIG header
    surely i can just copy the definition to my c file but it's wrong.
    on the other hand, i dont want to add thousands of definitions to my c file

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by amitbern View Post
    Well.. the obj file was smaller when removing the include file but the exe file was the same.
    i wonder if its the same for sys file also...
    thanks
    Also look into your compiler's optimization settings. Any significant optimization should guarantee that dead code is removed. There are certain situations where one wouldn't want "dead" code to be removed, for example if one was using a loop solely to cause delay, as in a timer, and so dead code removal may not be totally automatic.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i have a big header which i can't remove definitions from it
    > i need only 1 defnition in my c file, but i need to have it as small as possible
    How many things in say stdio.h or string.h do you use in a typical program. Probably just a few quite a lot of the time.

    Sure it may be just one now, but what about later on.

    Only those declarations which you actually use should make it into the final executable.

    It might take a few extra ms to compile, and some temp files will be a bit larger, but that's about it.
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, let's first say that the size of an object file is affected by much more than just what code is in there - for example, the compiler may put debug information in there, which will be more of when there's more code compiled - even if the code isn't actually used - but the debug information is just that, so it's not needed in the .exe or whatever.

    If you are writing a driver (.sys), then the header files are unlikely to actually contain any code that gets added to your object file - perhaps some inline functions, but those will only come into account when you use them, and if you do, you obviously need the header. It's a slightly different story if you build C++ code and you have a bunch of virtual functions, because the compiler may have a hard time figuring out what is used an what isn't. But C++ is rarely used in drivers for DOS/Windows etc.

    The other point is of course that if this is one of the object files belonging to YOU, rather than some standard file, you could always move the definition to somewhere else that you do include anyways.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-17-2008, 02:31 AM
  2. C - including the <windows.h> header file
    By Jolu42 in forum C Programming
    Replies: 4
    Last Post: 05-13-2007, 01:05 PM
  3. Including header file with in the header file
    By Arafat_211184 in forum C Programming
    Replies: 13
    Last Post: 12-19-2005, 10:03 AM
  4. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM