tags:

views:

62

answers:

3

I keep getting errors that my functions have been defined multiple times. Of course I only have one file and one function with that name in my file. Where could gcc find those other definitions?

Here is an example error message, but I get many of those:

/tmp/ccerCzAD.o:main.c:(.text+0xdb):

first defined here

/tmp/ccGbaGfJ.o: In function `at':

dlist.c:(.text+0xe3): multiple definition of `at'

I included "stdio.h" and "stdlib.h". Is the function "at()" maybe already defined in one of those?

+1  A: 

Function at seems to be defined in files dlist.c and main.c

Could this be the case?

file dlist.h

int at();

file dlist.c

int at(){return 0;}

file main.c

#include "dlist.h"

int at(){return 1;}
int main()
{
       return at();
}
Tom
Or maybe he put `int at(){return 0;}` in the header file? ??
pmg
Thanks for your answer. That was not my problem actually, but it made me realize my mistake. I accidentally put "dlist.c" instead of "dlist.h" in "main.c".
Lucas
+2  A: 

Maybe you're defining the function in the header file, as opposed to declaring it.

int at(void); /* declaration */
int at(void) { return 0; } /* definition */

The usual way is to put declarations in header files and definitions in code files.

pmg
I am going to mark this as the accepted answer, because it is basically what I did. Although I am not sure why in that case I get multiple definitions.
Lucas
After reading your comment to Tom's answer, I figure you're compiling `dlist.c` twice: once through a command line parameter and again through inclusion in `main.c`.
pmg
Yeah, that makes perfect sense. Thanks for helping me out.
Lucas
A: 

you might also want to:

#ifndef _DLIST_H
#define _DLIST_H

int at();

#endif

To prevent the same error when using #include to include the same header in multiple .c files.

James Morris