0

Just trying to figure out why I'm getting this error?

/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:21: multiple definition of `insert_at_tail'
img.o:/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:21: first defined here
ImageList.o: In function `printList':
/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:43: multiple definition of `printList'
img.o:/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:43: first defined here
ImageList.o: In function `make_empty_list':
/import/ravel/1/cjmu065/cs1921/ass2/src/ImageList.c:57: multiple definition of `make_empty_list'

The only files I have used to design these function names a header file and a subsequent implementation c file.

The header file includes these declarations:

void printList(ImageList *list);
ImageList *insert_at_tail(ImageList *list, char *name, QuadTree qtree, int dimen, int element);
ImageList *make_empty_list(void);

Whilst the implementation has this:

ImageList *insert_at_tail(ImageList *list, char *name, QuadTree qtree, int dimen, int element){
    node_t *new;
    new = malloc(sizeof(*new));
    assert(list!=NULL && new!=NULL);
    new->data.dim = dimen;
    new->data.num = element;
    new->data.filename = malloc(strlen(name)*sizeof(char));
    strcpy(new->data.filename, name);
    new->data.QuadTree = qtree;
    new->next = NULL;
    if(list->tail==NULL){
        list->head = list->tail = new;
    } else {
        list->tail->next = new;
        list->tail = new;
    }
    return list;
}

// print a list (space-separated, on one line)

void printList(ImageList *list)
{
        node_t *cur;

        for (cur = list->head; cur != NULL; cur = cur->next) {
                printf("%d",cur->data.num);
                printf(" [%2d]",cur->data.dim);
                printf(" %s",cur->data.filename);
        }
        putchar('\n');
}

// Make an empty list of images

ImageList *make_empty_list(void)
{
    ImageList *list;
    list = malloc(sizeof(*list));
    assert(list!=NULL);
    list->head = list->tail = NULL;
    return list;
}

I'm aware that the cause of this is usually from defining the function in the header file as well but it seems I haven't. I've looked through the files that I actually use these functions in but there are no extra definitions of the functions. The arguments and return values are also the same for both files so I'm kind of lost. Any help is appreciated.

CFLAGS=-Wall -g

img : img.o QuadTree.o ImageList.o
        gcc -o img img.o QuadTree.o ImageList.o

img.o : img.c QuadTree.h ImageList.h
        gcc $(CFLAGS) -c img.c

QuadTree.o : QuadTree.c QuadTree.h
        gcc $(CFLAGS) -c QuadTree.c

ImageList.o : ImageList.c ImageList.h
        gcc $(CFLAGS) -c ImageList.c

clean :
        rm -f img img.o QuadTree.o ImageList.o core

I added my Makefile, is the problem arising this? I also have guards on all the header files, so I'm still extremely confused, is there anything wrong with the declarations and definitions?

sebajun
  • 51
  • 8
  • 1
    Did you use safe guards? – gsamaras Oct 31 '14 at 04:21
  • Looks like you've got two object files (`ImageList.o` and `img.o`) which both define the functions? – Drew McGowen Oct 31 '14 at 04:21
  • Try changing the header file definitions to specify "extern" for the definitions – DrC Oct 31 '14 at 04:23
  • I tried changing to extern and that's doesn't help, but I think @Drew on the right track, I'm compiling and creating the object files from a makefile (which I haven't changed) that creates both of said object files. This only stopped working last night when I added some code to a file which includes the two files from the OP. http://www.cse.unsw.edu.au/~cs1921/14s2/assignment/src.zip Here is the framework of the setup, I have just changed some function definitions. – sebajun Oct 31 '14 at 05:21
  • 1
    follow @G.Samaras advice : in your header add safeguard to not include them twice. ie in every header xx #ifndef #HEADER_XXX #define HEADER_XXX ... do the includ things here #endif. This is usual way to protect against multiple inclusions. ( see any header sample ). – philippe lhardy Oct 31 '14 at 07:04

2 Answers2

0

By looking at zip file I can say a couple of things

1) Remove quadtree.h from ImageList.c Since you already included in ImageList.h

2) Make the function inline. In the quadtree.c in the function definition.

I believe this would solve the issue.

satyajanga
  • 121
  • 2
  • Thanks for the response. However, the makefile still runs when you don't change the contents of the unzipped file yeh? Which has quadtree.h in both imagelist.c and imagelist.h. I'm still going through all my files and finding no multiple definitions. – sebajun Oct 31 '14 at 06:28
0

Expanding a comment : use a safeguard in includes to not define twice the same things if file is included multiple times :

#ifndef HEADER_IMG_H
#define HEADER_IMG_H

void printList(ImageList *list);
ImageList *insert_at_tail(ImageList *list, char *name, QuadTree qtree, int dimen, int element);
ImageList *make_empty_list(void);

#endif

pattern for that is #ifndef HEADER_FILE_H #define HEADER_FILE_H ...here declarations... #endif.

philippe lhardy
  • 3,096
  • 29
  • 36