Thread: Help using Header Files

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    4

    Help using Header Files

    As my first post in the CProgramming forum i wanted to start off by saying hello and thanks in advance for any help I will receive.

    I am having trouble using header files. I am making a game, sort of like pac man, and I wanted to know how to make and use header files better. I have looked at several tutorials online but I cant seem to find anything that helps enough!!

    I basically wanted a little guideline on how to make header files so that I can have most of my algorithms for the game in there. Basically so I can keep my OpenGL code different to my code for controlling the game, e.g. functions I have for AI. I can post my code if it would help and any other advice is welcome.

    Thanks again!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You wouldn't put any of that into header files. Header files, in C, would (should, anyway) only contain function declarations, typedefs and struct definitions, and #defines. Look at say math.h in your standard library for an example.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    Header files don't contain any algorithms themselves, they would only contain declarations, typedefs, global variables, etc.

    So where do you put the algorithms themselves? you keep them in another .c file, which must be compiled and linked to your main file.

    e.g. Lets say we have a function WriteUsage() that we want to place in a separate file and add it using a header file. We'll have 2 extra files (of course the file names are just examples):
    1. writeusage.c
    2. writeusage.h
    Now, writeusage.c will contain the function body, and will look something like this:
    Code:
    #include "writeusage.h"    //You don't need to include this header, however it is preferable since you can keep all the standard headers you need inside it
    void WriteUsage(void)
    {
        printf("usage: program filename options\n");
    }
    writeuser.h will look like this:
    Code:
    #include <stdio.h>
    
    void WriteUsage(void)
    Your main file will look like something like this:
    Code:
    #include "writeusage.h"
    int main(int argc, char *argv[])
    {
        ...
        if (something's wrong with the arguments)
        {
            WriteUsage();
            do whatever you want
        }
        ...
        return 0;
    }
    Now when you want to compile it, you'll need to inform the compiler to compile wrtieusage.c, and the linker to link its object file to your program like this:
    Code:
    gcc main.c writeusage.c
    I might not be a pro, but I'm usually right

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Thnx alot Abda92! Should be able to use that to make my program a bit more manageable. Will give it a go and get back to you.

    As a note, im working on a mac using XCode. Would you know how I link through XCode! I'll have a go with it anyway and get back to you!

    Thnx alot again!

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Ok I have tried doing as you said, and I'm sorry if this is something really small that I'm doing wrong but I just cant see the problem so hopefully you can help me with this. Thnx in advance!

    My "robotusage.c" looks like this:
    Code:
    #include "robotusage.h"
    
    int incrementI(int num, int dir)
    {
     if (dir == 1)
      return num += 4;
     else return num -= 4;
    }
    My robotusage.h looks like this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <GLUT/glut.h>
    
    int incrementI(int num, int dir)
    The part of the main.c that is using this code is:
    Code:
    player.pointX = incrementI(player.pointX, 1);
    And it gives me an "error:syntax error before "{" token" in the robotusage.c file. I cant seem to find what I've done wrong.

    I would post all of the main.c as well but its quite big until I can sort this stuff out. Thnx again!

  6. #6
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    You need a semicolon in your .h file after your function prototype.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <GLUT/glut.h>
    
    int incrementI(int num, int dir);
    Last edited by JDGATX; 04-21-2008 at 01:59 PM. Reason: Because I am an idiot.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by d34n View Post
    My robotusage.h looks like this:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <GLUT/glut.h>
    
    int incrementI(int num, int dir)
    Put a semicolon at the end of that last line.

    gg

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    4
    Thnx alot guys! I hate those little things. Thnx again!

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int incrementI(int num, int dir)
    {
     if (dir == 1)
      return num += 4;
     else return num -= 4;
    }
    No sence to use += and -= here

    Code:
    int incrementI(int num, int dir)
    {
     if (dir == 1)
      return num + 4;
     
     return num - 4;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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