Thread: Error including .h files made by me

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

    Error including .h files made by me

    Hello everyone. I created this library:

    Code:
    //liberr.h
    void hello();
    and the correspondent .c :
    Code:
    //liberr.c
    #include <stdio.h>
    
    void hello(){
    	
    	printf("hello!!\n");
    	
    }
    then i made a simple program like this:
    Code:
    //errtest2.c
    #include "liberr.h"
    #include <stdio.h>
    
    int main(){
    
    	printf("testing...\n");
    	hello();
    	
    	return 0;
    }
    but i receive this error :
    "/tmp/ccub0rMj.o: In function `main':
    /home/paulo/Desktop/errtest2.c:7: undefined reference to `hello'
    collect2: ld returned 1 exit status"

    i'm using linux. what is the problem? thanks for your help

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    In this case you can compile with the following cmd:
    gcc errtest2.c liberr.c -o errtest2

    but I guess you wish to compile your liberr module separately, in this case:
    gcc -c liberr.c -o liberr.o
    gcc -c errtest2.c -o errtest2.o
    and to link everything:
    gcc errtest2.o liberr.o -o errtest2
    The two first cmd generate intermediary object code so that liberr is compiled once and for all.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Your fundamental problem is you think the .h file is the library. It's not -- the .c file is the library. The .h is just an attendant to it. You aren't linking with the library itself. The header file is a ghost.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    More than likely it's because you aren't linking the liberr.o file to the final executable. Just creating a header file won't tell the linker that you have multiple object files nor will it tell the compiler to compile the other file. If you are using gcc it would look like...

    gcc liberr.c errtest2.c -o hello

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files in .h
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 08:58 AM
  2. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  3. How do use home made header files
    By 0927 in forum C++ Programming
    Replies: 4
    Last Post: 01-10-2002, 09:16 PM
  4. home made header files...
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2001, 09:44 AM
  5. header .h files
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 09-03-2001, 09:26 PM