Thread: Header files

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    16

    Header files

    Hi! I've just created a program (.c) and a header (.h) file with all the functions that I use in my program. Of course I don't have any main() function in the header file, which gives me an error: "Undefined symbol _main in module c0.ASM" if I run the .h file. I've also added a line, telling
    Code:
    #include "functions.h"
    to the .c file and if I run the .c file, everything works without errors. So what is the correct way of creating a project? Should the .h file include a main function (which will result to multiple main() declaration) ?

    p.s. I'm using Borland C++ 3.1
    Last edited by nowber; 11-11-2009 at 03:28 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Header files are actually not necessary at all, but if you want to use one, go ahead.

    Anyway, no, there should not be a main() in the .h file. Each program can have only one main(). A header is not intended to used by itself.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Ok, so I have a functions.h header file (without the main function) and a main.c file. If I add both of them to a project and run it, I get multiple errors saying:

    _function1 defined in module MAIN.C is duplicated in module FUNCTIONS.H
    _function2 defined in module MAIN.C is duplicated in module FUNCTIONS.H
    etc...

    although I have defined them ONLY in functions.h file. The same error is produced for the array of records, that I've defined in functions.h. Any idea of what I'm doing wrong?

    Here's a small example how it looks in general:

    Code:
    (functions.h)
    
    int number (int low, int high);
    
    struct rec {
    	char name[33];
    	};
    struct rec arr[5]; // array of records
    
    
    int number (int low, int high){
    ... some actions
    }
    
    (main.c)
    
    #include "functions.h"
    
    number(1,50)
    Last edited by nowber; 11-11-2009 at 04:08 PM.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by nowber View Post
    If I add both of them to a project and run it,
    I am not familiar with the Borland compiler, so I presume this means you are doing something with the IDE?

    Anyway, normatively you just include the .h file in your .c file:
    Code:
    #include "myheader.h"
    Note that is not <myheader.h>. This presumes both files are in the same directory.

    Then you just compile the .c file and the .h file will be included.

    [edit] okay, it looks like that is what you are already doing....hmmm
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to make the best use of header files
    By alanb in forum C++ Programming
    Replies: 4
    Last Post: 08-31-2009, 04:45 PM
  2. what am I missing? (Program won't compile)
    By steals10304 in forum C Programming
    Replies: 3
    Last Post: 08-25-2009, 03:01 PM
  3. Header files and multiple definitions
    By sjweinberg in forum C++ Programming
    Replies: 16
    Last Post: 07-17-2009, 05:59 PM
  4. #include header files or .cpp files?
    By DoctorX in forum C++ Programming
    Replies: 3
    Last Post: 12-23-2006, 12:21 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM