Lucas
Lucas

Reputation: 14129

What could be the reason for getting the error "multiple definitions" while linking?

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?

Upvotes: 1

Views: 548

Answers (3)

James Morris
James Morris

Reputation: 4955

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.

Upvotes: 0

pmg
pmg

Reputation: 108986

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.

Upvotes: 2

Tom
Tom

Reputation: 45174

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();
}

Upvotes: 1

Related Questions