C header files?

This is a discussion on C header files? within the C Programming forums, part of the General Programming Boards category; hi, I ve questions bout header files... for example if I have main function like below, how can i separate ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    C header files?

    hi,

    I ve questions bout header files...
    for example if I have main function like below, how can i separate it into .c and .h files, to make it neat and more efficient..

    Code:
    #include <stdio.h>
    
    #define  ASIZE        5000
    
    int number = 0; /* Global int declaration */
    
    struct data
    {
    	int  key;
    	char *word;
    } Data;
    
    	/* Functions Prototype */
    
    void function1();
    int function2 ();
    void function3();
    
                  /* Main Program */
    
    int main( void )
    {
          // main program here......
         // and here here here....etc etc
    
          return 0;
    
    }
    
                  /* Functions */
    
    void function1()
    {
    
        return;
    }
    
    int function2()
    {
        .....
    }
    
    void function3()
    {
    
    }
    is that posibble to separate all the functions and the main program (into separate .c files)?? if yes, how can i link both of them when compiling?

    thanks in advance,

    CyC|OpS

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Create a header file for the function prototypes and data declaration, a source file for the function definitions, and a source file for your main.

    functions.h
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    
    #define  ASIZE        5000
    
    int number = 0; /* Global int declaration */
    
    struct data
    {
    	int  key;
    	char *word;
    } Data;
    
    	/* Functions Prototype */
    
    void function1();
    int function2 ();
    void function3();
    
    #endif /* FUNCTIONS_H */
    functions.c
    Code:
    #include "functions.h"
    
                  /* Functions */
    
    void function1()
    {
    
        return;
    }
    
    int function2()
    {
        .....
    }
    
    void function3()
    {
    
    }
    maindriver.c
    Code:
    #include <stdio.h>
    #include "functions.h"
    
                  /* Main Program */
    
    int main( void )
    {
          // main program here......
         // and here here here....etc etc
    
          return 0;
    }
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    hi David,

    thanks for your explanation...very clear

    now how to compile it?
    issit only calling the main.c

    eg: gcc main.c

    or i need to call the dependecies too? .h and functions.c?

    thanks...

    CyC|OpS

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    There's a couple of ways, here's one simple version:

    >gcc maindriver.c functions.c
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    16
    so in this case, inside maindriver.c
    in the main function
    can I still use the functions from function.c? when I have only included function.h?
    eg

    #include<stdio.h>
    #include "function.h"

    int main (void)
    {
    void function1();
    return;
    }


    Oh, btw, how do you type in codes in this forum properly?
    like you guys have
    code:
    ------------------
    the codes
    ------------------

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >now how to compile it?

    A useful method is using make-files. See a guide to using makefiles here:
    http://www.delorie.com/djgpp/doc/ug/...makefiles.html

    >can I still use the functions from function.c? when I have only
    >included function.h?

    Yes, but you can use the function only if function.c is linked with maindriver.c.

    >Oh, btw, how do you type in codes in this forum properly?

    It's like this, but then without the spaces between it:

    [ code ]
    /* Code */
    [ / code ]

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>>Oh, btw, how do you type in codes in this forum properly?

    http://www.cprogramming.com/cboard/s...threadid=13473
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    Code:
    #ifndef FUNCTIONS_H
    #define FUNCTIONS_H
    
    /* function here...*/
    
    #endif
    how is this #ifndef & #endif work??
    do i need to call FUNCTION_H inside the maindriver.c? or inside function.h? coz i didnt see any connection between maindriver.c or funtion.h with functions.c

    thanks,

    Ferdinand

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    These are preprocessor directives. It prevents from including the header-file more then once.

    In normal language your code is as follows:
    If FUNCTIONS_H is not defined, then define FUNCTIONS_H. Include the code until #endif is reached. The second time the file is passed, FUNCTIONS_H is already defined, so FUNCTIONS_H does not need to be defined anymore and the code will not be included.

    >do i need to call FUNCTION_H inside the maindriver.c?

    FUNCTIONS_H is a defined symbol, it is not a function which you can call.

    ####

    Example:

    I have a file called test.c in which a function test() is defined which displays a text on screen. The file is as follows:

    Code:
    /*TEST.C*/
    #include <stdio.h>
    #include "test.h"
    
    void test ()
    {
        printf ("This is a test\n");
    }
    The header-file contains the prototype of test().

    Code:
    /*TEST.H*/
    #ifndef TEST_H
    #define TEST_H
    
    void test ();
    
    #endif
    This all is used by the main() function.

    Code:
    /*MAIN_TEST.C*/
    #include "test.h"
    
    int main (void)
    {
        test ();
        
        return 0;
    }
    Hope this explains a little.

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    thanks Shiro

    thats explain alot...

    I just finished separating my program...
    the skeleton is exactly the same as Strider post above..

    however, when i compile it

    > gcc maindriver.c functions.c

    it gives me hell alot of parse errors and variables not declared (in functions.c)

    I also try to compile using this
    > gcc -c maindriver.c
    > gcc -c functions.c
    > gcc -g -Wall -o progname maindriver.o functions.o

    which's also not working =(

    regarding some answer in previous post before, it said that i still can use the functions.c IF it is linked with the maindriver.c, how to link it?

    and more, if I ve 2 programs, 2 program which has its each main(),
    and after separating :
    -prog #1:
    separated into => maindriver1.c -> dependecies functions1.c and functions1.h

    -prog #2
    separated into => maindriver2.c -> dependecies functions2.c and functions2.h

    issit possible to link all of them into 1 executables..?

    thanks...

    CyC|OpS
    Last edited by CyC|OpS; 09-14-2002 at 08:27 PM.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    anyone can help? I ve worked on it for the whole night and still can't figure it out...

    *_*

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >it gives me hell alot of parse errors and variables not declared (in functions.c)
    Try compiling functions.c on it's own like this
    >gcc -c functions.c
    Fix the errors that come out of that compile. If you can't post some more code here, along with the error messages. Then work on compiling maindriver.c, then linking them.

    >>and more, if I ve 2 programs, 2 program which has its each main(),
    There can be only one main() function if you're making one executable. Compile and link them just like you have been doing.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    there's no error when i compile the object files
    > gcc -c maindriver.c
    > gcc -c functions.c

    however it gives errors when
    > gcc -g -Wall -o progname maindriver.o functions.o

    the prog working before separated into functions.c and functions.h...

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    My guess (and without seeing your code, that's all it is), is that you have variable(s) declared in the header file and they're being highlighted as duplicates when you perform the link.

    Post the header file.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    25
    exactly!!!

    I ve one variable which i declared as global variable inside header file..and i didnt declared it in other source file...

    int var = 2;

    how can i overcome this prob??

    CyC|OpS

Page 1 of 2 12 LastLast
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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21