Thread: multiple definition??

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

    multiple definition??

    I keep getting this error when I try to compile three files at once:
    gcc table.h table.c tabletest.c

    tabletest.c:5: warning: return type defaults to âintâ
    tabletest.c: In function âmainâ:
    tabletest.c:10: warning: unused variable âpâ
    /tmp/ccIOnFbt.o: In function `newTable':
    /home/cc/cs61cl/fa09/class/cs61cl-bo/hw3/table.c:23: multiple definition of `newTable'
    /tmp/ccwhxUwU.o:/home/cc/cs61cl/fa09/class/cs61cl-bo/hw3/table.c:23: first defined here
    collect2: ld returned 1 exit status

    table.h
    Code:
    struct table;
    	
    struct table *newTable ( );
    table.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "table.h"
    
    struct table {
           int entrycount;
    }myTable;
    	
    /* returns an empty table */
    struct table * newTable ( ) {
        //struct table * table1 = malloc(sizeof(struct table));
           myTable.entrycount = 0;
           return &myTable;       
     }
    tabletest.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "table.c"
    
    main() {
    
        struct table t;
        t.entrycount = 1;
        struct table *p = newTable();
        printf("new address %d\n", p);
        return 0;
    
    }
    Sorry if this is a really obvious error, but I have a pretty simple header set up, but why is it not compiling? Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1. You don't generally #include .c files. You include header files, and compile and link .c files.
    2. Why do you have "struct table;" in your header? What are you trying to do there?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    ok I fixed the table.c --> table.h but now I'm getting a "storage size of t isn't known" for the line "struct table t" in my tabletest.c

    I thought I had to put the struct table; line in the header because I'm defining it too in the table.c?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    /* table.h */
    struct mytable
    {
        ...stuff...
    };
    
    void myfunction( void ); /* prototype */
    
    /* table.c */
    #include<stuffineed.h>
    #include"table.h"
    
    void myfunctionprototype( void ) /* actual function */
    {
        ...do stuff, presumably stuff that requires our structure...
    }
    
    
    /* somethingelse.c */
    #include<stuffineed.h>
    #include"table.h"
    
    ...
    
    insomefunction()
    {
        ...doing stuff...
        myfunction( ); /* can access this because it's a prototype in a header i've included... */
    
    }
    That's generally how it works.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    16
    ah ok that's what I'd thought. thanks for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM