Reputation: 240
I have tried to include pretty much every combination of headers. Trying to compile the object files by using:
gcc database.c -c
gcc database.o user_interface.c -o database
The result is:
/tmp/ccWfp2tS.o: In function `addRecord':
user_interface.c:(.text+0x4b9): multiple definition of `addRecord'
database.o:database.c:(.text+0x0): first defined here
/tmp/ccWfp2tS.o: In function `printAllRecords':
user_interface.c:(.text+0x54a): multiple definition of `printAllRecords'
database.o:database.c:(.text+0x1a): first defined here
/tmp/ccWfp2tS.o: In function `findRecord':
user_interface.c:(.text+0x590): multiple definition of `findRecord'
database.o:database.c:(.text+0x24): first defined here
/tmp/ccWfp2tS.o: In function `deleteRecord':
user_interface.c:(.text+0x5ed): multiple definition of `deleteRecord'
database.o:database.c:(.text+0x36): first defined here
collect2: error: ld returned 1 exit status
user_interface.c
#include "record.h"
int debugmode;
int main(int argc, char *argv[])
{
// my code here
}
// other functions: AddRecord, printallRecords,...
database.h
#include "record.h"
int addRecord (struct record **, int, char [ ],char [ ]);
void printAllRecords(struct record *);
int findRecord (struct record *, int);
int deleteRecord(struct record **, int);
database.c
#include "database.h"
extern int debugmode;
int addRecord(struct record ** start, int account, char name[], char address[])
{
/*my code here*/
return 0;
}
/*other functions...*/
record.h
struct record
{
int accountno;
char name[25];
char address[80];
struct record* next;
};
Upvotes: 2
Views: 12927
Reputation: 100836
You seem to be having a lot of troubles because you're down in the weeds, throwing things at the wall. But it's not really that hard if you step back and look at it from a higher level.
The concept of interest here are declarations vs. definitions. It's unfortunate they have such similar names: it's easy to get them confused.
Think of a definition like your house, and a declaration like the address of your house. You only have one of your house, but you can hand out your address to lots of different people. Your address tells people how to get to your house, but it isn't actually your house.
Similarly in C, a definition of a function is the actual implementation of the function, and the declaration of the function describes how to call the function.
A declaration looks like this:
void printAllRecords(struct record *);
(note, no body of the function, just the way it's called) and a definition looks like:
void printAllRecords(struct record *record)
{
...do some stuff...
}
(note, now you have the body of the function)
You normally put the declaration in a header file. Then anyone who wants to call your function will #include
that header file, so that they know the right way to call it.
But you don't want to #include
the definition of a function (for example, by #include
ing the source file) because that would mean you have two implementations of the same function, and that's not possible just like there can't be two of your house.
So, in short:
#include
the header file containing the declaration in the source file containing the definition, so the compiler will complain if they don't match#include
the header file containing the declaration in that source file tooIn your case, all the functions defined in database.c
should have declarations in database.h
, then any other source file (like user_interface.c
) should #include "database.h"
to see those declarations.
Upvotes: 6