Thread: My own header files

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question My own header files

    Hi!

    My question is how to make my own header files? I' m writing a program and I want that every function will be in his own .c file. Something like this:
    main.c
    main.h
    about.c
    about.h
    ...

    I was trying something but I always get a lot of errors .

    Please help.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    Don't know if you have tried this already >

    Create the header file, say example.h, using
    #ifndef EXAMPLE_H
    #define EXAMPLE_H

    at the beginning

    and
    #endif

    at the end


    call the header file then from your main file using

    #include "example.h"

  3. #3
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Unhappy

    Your main.c file has to include all the headers of the functions you are trying to use.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I was trying something but I always get a lot of errors
    Yeah, posting them would have been a big help

    Unless you want another 20 messages guessing...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    88

    Try This...

    create afile main.h
    write there your function

    then create your main.c
    write there your program
    and at the top write

    #include "main.h"

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669


    My main.c file:
    Code:
    # include "main.h"
    
    int main()
    {
    // few functions and they are defined in main.h
    }
    main.h file:
    Code:
    # include <windows.h>
    # include <stdio.h>
    # include <conio.h>
    # include <string.h>
    
    # define LEFT_CTRL  40
    # define LEFT_ALT   34
    # define RIGHT_ALT  33
    # define RIGHT_CTRL 36
    
    void clrscr (void);
    
    void clrscr ()
    {
      system ("CLS");
    }
    about.c file:
    Code:
    # include "main.h"
    # include "about.h"
    
    void About ()
    {
     // code
    }
    about.h file:
    Code:
    void About (void);
    These are my 4 files. I get an error that function clrscr () is already defined in main.obj.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  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
    Code:
    void clrscr ()
    {
      system ("CLS");
    }
    Move this to main.c

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Is there no way to have a clrscr in a main.h file? And for the # define I too get errors that they are already defined in main.obj file. Should I moved them too into main.c file?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    void clrscr (void);
    This in the .h file

    void clrscr ()
    {
    system ("CLS");
    }
    This in the .c file

    Declarations (#defines, prototypes etc) go in .h files

    Definitions go in .c files

    You managed it with your about function.


    All .h files should begin with
    #ifndef FILE_INCLUDED
    #define FILE_INCLUDED

    And end with
    #endif

    Eg
    Code:
    #ifndef MAIN_H
    #define MAIN_H
    # include <windows.h>
    # include <stdio.h>
    # include <conio.h>
    # include <string.h>
    
    # define LEFT_CTRL  40
    # define LEFT_ALT   34
    # define RIGHT_ALT  33
    # define RIGHT_CTRL 36
    
    void clrscr (void);
    #endif MAIN_H
    This protects you from including the same .h file several times

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    One more question. What about constants (const int x = 5?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    extern const int x;
    Would go in a .h file


    const int x = 5;
    would go in a .c file

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Is there anything else that I should know about the header files?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  3. classes and header files
    By Drake in forum C++ Programming
    Replies: 8
    Last Post: 11-30-2006, 07:12 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