Thread: Simple problem with header file ..

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    47

    Simple problem with header file ..

    Hi, been some time since I programmed in c, so I'm sitting and coding some but I can't seem to understand what I do wrong ... my code:

    foo.h:
    Code:
    #ifndef FOO_H_
    #define FOO_H_
    
    extern int getage(char *name);
    
    #endif
    foo.c:
    Code:
    #include "foo.h"
    
    int getage(char *name);
    
    int getage(char *name){
    	return 10;
    }
    wibble.c:
    Code:
    #include "foo.h"
    #include <stdio.h>
    
    int main(){
    	printf("Fred's age: %d\n", getage("Fred"));
    	return 0;
    }

    When I compile: gcc wibble.c -o wibble I get:

    /tmp/ccWbHHtl.o: In function `main':
    wibble.c.text+0xa): undefined reference to `getage'
    collect2: ld returned 1 exit status

    What am I doing wrong?

    thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    You are not compiling/linking foo.c into your project.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    47
    D'OH!

    thanks m8.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include "foo.h"
    
    int getage(char *name);
    
    int getage(char *name){
    	return 10;
    }
    Since you are including foo.h (and you should - it makes sure that you don'd mismatch your foo.h prototype and getage's definition), you should not prototype getage() as well as define it.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need help understanding Header Files
    By Kaidao in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2008, 10:02 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Simple End of file problem
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2002, 01:05 PM