-
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.
-
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.
-
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?
-
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.
-
ah ok that's what I'd thought. thanks for your help!