Thread: Creating header files

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    25

    Question Creating header files

    As part of one of my projects, I have to create a program that doesn't use any header files except ones that I created myself. Even cout and cin is out. i have looked through a few of the header files that came with my compiler and can't understand a single thing in them. Does any body know where I can learn to write (or at least read) a header file.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    This is rather a bad thing to do. Do NOT try to just manually include pieces of header files. Most are written in compiler and system specific methods, that if used in the wrong way, will result in undefined behavior.

    Could you give some more detail about what you're trying to do?

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    File->Save as-> Filename.h

    Headers are the same as .cpp files except they do not include a 'main' statement.

    This is a valid header file:

    Code:
    int Thing(int a) {
     return a - 1
    }
    To include it, put it in the same directory as your .cpp file and you would use;
    Code:
    #include "Filename.h"
    Some header files are obfuscated. (removes any uneccesary formatting but keeps the actual code in tact) I don't reccomend trying to understand them if you are just starting C++ anyways.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Blackroot View Post
    Headers are the same as .cpp files except they do not include a 'main' statement.
    That's the worst definition of a header file I've ever seen.

    This is a valid header file:
    Valid? That's subjective. Wrong? Definitely.

    Some header files are obfuscated. (removes any uneccesary formatting but keeps the actual code in tact) I don't reccomend trying to understand them if you are just starting C++ anyways.
    It just gets worse and worse...

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    He's trying to write an OS.

    And I'm telling you now, you're so way out of your depth that you can't even see the bottom. Learn the actual language first. Your questions so far show that you are struggling with the basics of C++, yet you want to one of the most complicated things you can do as a programmer for a learning project.

    I say, forget it. Write normal programs in a nice, hosted environment, until you actually know the language. What you're doing right now is pointless.
    When you actually know the language, then you can return to your OS ambitions. But before that, you also need to learn a lot about computer architecture and related topics.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by CornedBee View Post
    He's trying to write an OS.
    What makes you think he's trying to write an OS?

    It's easy to write programs that don't include any standard header files (although the program's functionality would be severely limited).

    Code:
    int main( int argc, char* argv[] )
    {
       int i = 0;
       char* str = argv[1];  // Assume user always runs program correctly (with 1 argument).
    
       for ( i = 0; str != 0; ++i, ++str );
    
       return i;  \\ Return length of argument.
    }

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    What makes you think he's trying to write an OS?
    Another thread by the same poster.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Another three threads by the same poster, to be exact.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Couldn't he (or she) just make a header file like this (if he's allowed, of course):
    Code:
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    using namespace std;
    Would that work (as in, will that work for you)?
    Last edited by mikeman118; 10-16-2007 at 07:06 PM.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Again: the OP wants to create an operating system. The restriction on not using any system headers comes not from braindead exercise limitations, but from the simple fact that his own OS wouldn't have these headers unless he implemented them first.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, lets clarify something here.

    There is no point in not using someone elses header files, if you don't also implement the functionality needed in some other way. Say for example we can't use stdio.h: Ok, so we can't do printf/scanf - but we could of course write a more or less complete other implementation of printf/scanf to use in our application, and the related header-file(s) to cope with that.

    But there's no magic involved - the original poster appears to think that header files are "magic" in some way. They are not.

    A header-file contiains declarations:
    * External function declarations.
    * External Variable declarations.
    * Constant declarations (defines and const int x = ...)
    * Type declarations (struct x, typedef, union y, class z, etc)

    This is used to communicate common information between the calling and called functions. For example, in the Linux system call open("somename.txt", O_RDONLY), we need a O_RDONLY to be defined in the same way in both the calling code and the function definition of open - strange behaviour would happen if O_RDONLY is 3 in one place and 6 in the other.

    Now, the original poster obviously needs to come up with a set of functionality that the OS should support, split it into nice little chunks (e.g. file I/O, interprocess communication, time management, driver functions etc) and decide what data structures & types are needed, constants and what functions are needed to support this functionality. The implement that as a set of header files and a set of functions to actually perform the operation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    See if this helps: Write your own OS

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    No need to fight.

    I'll go back and study the basics a bit longer.

    Thanks.

  14. #14
    Registered Abuser
    Join Date
    Sep 2007
    Location
    USA/NJ/TRENTON
    Posts
    127
    I've fooled around with writing OS's a bit, here's a great place to get started:

    http://www.osdev.org

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    25
    I've already been examing that site a bit. I think your the fourth one who gave it to me. It is coming in handy though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help creating multiple text files
    By Tom Bombadil in forum C Programming
    Replies: 19
    Last Post: 03-28-2009, 11:21 AM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  3. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM