Thread: Multiple definition of ' ' first defined here

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    16

    Multiple definition of ' ' first defined here

    So I implemented a linked list and a separate chaining hashtable of a struct called Objective, so that I can implement some functions to work with that struct.

    Hashtable.c:

    Code:
    #include <stdlib.h>#include <string.h>
    #include <stdio.h>
    #include "HASHTABLE.h"
    
    #define hash(A,B) (A%B)
    
    static link *heads;
    static int M;
    
    void Init(int m){
        int i;
        M = m;
        heads = (link*)malloc(M*sizeof(link));
        for(i = 0; i < M; i++)
            heads[i] = NULL;
    }
    
    pObjective search(unsigned long id){
        int i = hash(id, M);
        return searchList(heads[i], id);
    }
    
    void insert(pObjective o){
        int i = hash(o->id, M);
        heads[i] = insertBegin(heads[i], o);
    }
    
    void delete(unsigned long id){
        int i = hash(id, M);
        heads[i] = removeList(heads[i], id);
    }
    
    link insertBegin(link h, pObjective obj){
    
    link new = (link)malloc(sizeof(struct nodehash));
    new->obj = obj;
    new->next = h;
    return new;
    
    }
    
    pObjective searchList(link h, unsigned long id){
        link t, x;
        for(t = h; t != NULL; t = t->next){
            if(t->obj->id == id)
                x = t;
        }
        return x->obj;
    
    }
    
    link removeList(link h, unsigned long id){
        link t, x, z;
        for(t = h; t != NULL; t = t->next){
            if(t->next->obj->id == id)
                x = t;
        }
        z = x->next;
        x->next = z->next;
        free(z);
        return h; }
    Hashtable.h:

    Code:
    #ifndef HASHTABLE_H#define HASHTABLE_H
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Objective{
        char name [8000];
        unsigned long id, duration, deps [9000];
        int hasDeps;
    }*pObjective;
    
    typedef struct nodehash{
        pObjective obj;
        struct nodehash*next;
    }*link;
    
    
    void Init(int M);
    pObjective search(unsigned long id);
    void insert(pObjective o);
    void delete(unsigned long id);
    link insertBegin(link h, pObjective obj);
    pObjective searchList(link h, unsigned long id);
    link removeList(link h, unsigned long id);
    
    
     #endif
    List.c:

    Code:
    #include <stdlib.h>#include <string.h>
    #include <stdio.h>
    #include "LISTA.h"
    
    
    
    links insertEnd(links head, lObjective obj){
    
        links t;
        links new = (links)malloc(sizeof(struct node));
        new->obj = obj;
        new->next = NULL;
        if(head == NULL) return new;
        for(t = head; t->next != NULL; t = t->next)
            ;
        t->next = new;
        return head;
    }
    
    int checkId(links head, unsigned long id){
        links t;
        int count = 0;
        for(t = head; t != NULL; t = t->next){
            if(t->obj->id == id)
                count++;
        }
        return count;
    }
    
    links removeList2(links head, unsigned long id){
        links t, x, z;
        for(t = head; t != NULL; t = t->next){
            if(t->next->obj->id == id)
                x = t;
        }
        z = x->next;
        x->next = z->next;
        free(z);
        return head;
    }
    
    void print(links head){
        links t;
        int i;
    
        for(t = head; t!=NULL; t = t->next){
            printf("%lu %s %lu ", t->obj->id, t->obj->name, t->obj->duration);
            for(i = 0; i < 9000; i++){
                printf("%lu ",t->obj->deps[i]);
            }
            printf("\n");
        } }
    List.h:

    Code:
    #ifndef LISTA_H#define LISTA_H
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Objectives{
        char name [8000];
        unsigned long id, duration, deps [9000];
        int hasDeps;
    }*lObjective;
    
    typedef struct node{
     lObjective obj;
     struct node *next;
    }*links;
    
    links head = NULL;
    
    links insertEnd(links head, lObjective obj);
    int checkId(links head, unsigned long id);
    links removeList2(links head, unsigned long id);
    void print(links head);
    
    #endif
    Objective.c:

    Code:
    #include <stdlib.h>#include <string.h>
    #include <stdio.h>
    #include "OBJECTIVES.h"
    
    
    int existsDep(unsigned long dep [9000]){
        int count = 0, i;
    
        for(i=0; i < 9000; i++){
            if(checkId(head, dep[i]) == 0)
                count++;
        }
        if(count == 0)
            return 1;
        else
            return 0;
    }
    
    void newObjective(unsigned long id, char name [8000], unsigned long duration,
      unsigned long dep [9000]){
    
        int i; 
    
        pObjective obj = malloc(sizeof(pObjective));
        lObjective ob = malloc(sizeof(lObjective));
        obj->id = id;
        ob->id = id;
        obj->duration = duration;
        ob->duration = duration;
        obj->hasDeps = 1;
        ob->hasDeps= 1;
        strcpy(name, obj->name);
        strcpy(name, ob->name);
    
        for(i = 0; i < 9000; i++){
            obj->deps[i] = dep[i];
            ob->deps[i] = dep[i];
        }
    
        if(checkId(head, id) != 0)
            printf("id already exists\n");
        else if(existsDep(dep) == 0){
            printf("no such task\n");
        }    
        else{
            insert(obj);
            insertEnd(head, ob);
        }
        free(obj);
        free(ob);
    }
    
    void newObjectiveNoDeps(unsigned long id, char name [8000], unsigned long duration){
    
        pObjective obj = malloc(sizeof(pObjective));
        lObjective ob = malloc(sizeof(lObjective));
        obj->id = id;
        ob->id = id;
        obj->duration = duration;
        ob->duration = duration;
        obj->hasDeps = 0;
        ob->hasDeps = 0;
        strcpy(name, obj->name);
        strcpy(name, ob->name);
    
         if(checkId(head, id) != 0)
            printf("id already exists\n");
         else{
            insert(obj);
            insertEnd(head, ob);
        }
        free(obj);  
        free(ob);
    }
    
    void removeObj(unsigned long id){
        pObjective obj = search(id);
    
        if(obj->hasDeps == 1)
            printf("task with depedencies\n");
        else if(checkId(head, obj->id) == 0)
            printf("no such task\n");
        else
            delete(id);
            removeList2(head, id);
    }
    
    void depend(unsigned long id){
        pObjective obj = search(id);
        int i;
    
        if(obj->hasDeps == 0)
            printf("%lu: no depedencies\n", id);
        else if(checkId(head, obj->id) == 0)
            printf("no such task\n");
        else{
            printf("%lu: ",id );
            for(i = 0; i < 9000; i++){
                printf("%lu ", obj->deps[i]);
            }
        }
        printf("\n");
    }
    
    void duration(){
        print(head);
    }
    Objective.h:

    Code:
    #ifndef OBJECTIVES_H#define OBJECTIVES_H
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "LISTA.h"
    #include "HASHTABLE.h"
    
    int existsDep(unsigned long dep [9000]);
    void newObjective(unsigned long id, char name [8000], unsigned long duration,
      unsigned long dep [9000]);
    void newObjectiveNoDeps(unsigned long id, char name [8000], unsigned long duration);
    void removeObj(unsigned long id);
    void depend(unsigned long id);
    void duration();
    
    #endif
    My problem is in the head of the list, i keep getting this error:

    Code:
    /tmp/ccIa32rv.o:(.bss+0x0): multiple definition of `head'/tmp/ccAvumDO.o:(.bss+0x0): first defined here
    /tmp/ccuVImET.o:(.bss+0x0): multiple definition of `head'
    /tmp/ccAvumDO.o:(.bss+0x0): first defined here
    collect2: error: ld returned 1 exit status
    And i don't know where I'm wrong... can anyone help me?


  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,911
    Basically, you messed up with your use of global variables, e.g., you declared head as a global variable in a header file in a way that makes it such that the header cannot be included in more than one source file. Turn your global variables into local variables (possibly declared in the main function) and your problem with disappear.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    If I put links head = NULL in the main function it gives the error that is a unused variable, and all those heads I use in Obejctives.c gives the error that they are undeclared

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,911
    Quote Originally Posted by PTM
    If I put links head = NULL in the main function it gives the error that is a unused variable, and all those heads I use in Obejctives.c gives the error that they are undeclared
    Hence all the functions that currently use the head global variable in Objectives.c should have a parameter named head instead.

    That said, there may be another way out. Move this:
    Code:
    links head = NULL;
    to the top of Objectives.c (but after the header inclusions) and change it to:
    Code:
    static links head = NULL;
    From what I see, this global variable named head is basically an implementation detail of your Objectives related functions, so while not ideal, giving it internal linkage in Objectives.c while retaining file scope may be acceptable. Still, a file scope variable named such a common name as "head" is a poorly named one: generally the wider the scope, the more descriptive the name should be.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    Thanks a lot, it solved the problem. It got to this point because I was testing my functions from Objectives.c and it was always giving segmentation fault and I thought the problem was that I did not initialize the head to NULL. But its still giving me segmentation fault... I'm testing by hand in the main function because I still don't have the commands:

    Code:
    #include <stdlib.h>
    #include <string.h>
    #include <stdio.h>
    #include "OBJECTIVES.h"
    
    
    int main(){
        unsigned long array [1] = {3};
        Init(11);
        newObjectiveNoDeps(1, "tarefa1", 20);
        newObjective(2, "tarefa2", 20, array);
        return 0;
    }
    I really don't know what is giving me seg fault... it's the Hashtable or the Linked List but i think i have them both well implemented... can you help me figure it out?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,911
    Hmm... why do you have these two as separate struct types?
    Code:
    typedef struct Objective{
        char name [8000];
        unsigned long id, duration, deps [9000];
        int hasDeps;
    }*pObjective;
    
    typedef struct Objectives{
        char name [8000];
        unsigned long id, duration, deps [9000];
        int hasDeps;
    }*lObjective;
    If I were you, I would just declare:
    Code:
    typedef struct Objective {
        char name[8000];
        unsigned long id, duration, deps[9000];
        int hasDeps;
    } Objective;
    Then when I need a pointer to an Objective, I just write Objective* instead of a pointer typedef. I recommend against pointer typedefs unless the pointer is an opaque pointer.

    Likewise, why do you have essentially the same thing repeated for nodes?
    Code:
    typedef struct nodehash{
        pObjective obj;
        struct nodehash*next;
    }*link;
    
    typedef struct node{
     lObjective obj;
     struct node *next;
    }*links;
    If I were you, I would just declare:
    Code:
    typedef struct ObjectiveNode {
        Objective *obj;
        struct ObjectiveNode *next;
    } ObjectiveNode;
    Having a single node type is likely to be less confusing than having two similiarly structured node types with names that are so similiar.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    Yeah you're, is just that I'm new at C and it still confuses me. I want a linked list and a hashtable and it was confusind trying to declare a node and struct to use in both, i eventually would mess up. I'm gonna try to change and see if it works, thanks!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    25,911
    Ah, I see. Another thing that might help, since you're doing a redesign anyway: declare a struct type for the hash table, e.g.,
    Code:
    typedef struct ObjectiveHashTable {
        ObjectiveNode **slots;
        int num_slots;
    } ObjectiveHashTable;
    
    int ObjectiveHashTableInit(ObjectiveHashTable *table, int m) {
        int i;
        table->num_slots = m;
        table->slots = malloc(m * sizeof(table->slots[0]));
        if (!table->slots) {
            return 0;
        }
        for (i = 0; i < m; i++) {
            table->slots[i] = NULL;
        }
        return 1;
    }
    Notice that ObjectiveHashTableInit is basically your Init function, except that it has a more descriptive name and a pointer parameter for the hash table. Furthermore, instead of a void return type, I changed it to return an int that is basically a boolean value reporting if the initialisation succeeded or failed. This way, you can write code to check that, and while unlikely, it is theoretically possible that a failed malloc could happen, and this would have led to a segfault.
    Last edited by laserlight; 05-18-2018 at 04:58 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2018
    Posts
    16
    I'm getting very confused... don't know where to use each variable, if its an Objectivenode or an Objective, where do I declare them, how to change the previous functions... i got to here:

    Hashtable.c:

    Code:
    #include <stdlib.h>#include <string.h>
    #include <stdio.h>
    #include "HASHTABLE.h"
    
    #define hash(A,B) (A%B)
    
    static Objectivenode *heads;
    static int M;
    
    void Init(int m){
    	int i;
    	M = m;
    	heads = (Objectivenode*)malloc(M*sizeof(Objectivenode));
    	for(i = 0; i < M; i++)
    		heads[i] = NULL;
    }
    
    Objectivenode search(unsigned long id){
    	int i = hash(id, M);
    	return searchList(heads[i], id);
    }
    
    void insert(Objective o){
    	int i = hash(o->id, M);
    	heads[i] = insertBegin(heads[i], o);
    }
    
    void delete(unsigned long id){
    	int i = hash(id, M);
    	heads[i] = removeList(heads[i], id);
    }
    
    Objectivenode insertBegin(Objectivenode h, Objective obj){
    
    Objectivenode new = (Objectivenode)malloc(sizeof(struct Objectivenode));
    new->obj = obj;
    new->next = h;
    return new;
    
    }
    
    Objective searchList(Objectivenode h, unsigned long id){
    	Objectivenode t, x;
    	for(t = h; t != NULL; t = t->next){
    		if(t->obj->id == id)
    			x = t;
    	}
    	return x->obj;
    
    }
    
    Objectivenode removeList(Objectivenode h, unsigned long id){
    	Objectivenode t, x, z;
    	for(t = h; t != NULL; t = t->next){
    		if(t->next->obj->id == id)
    			x = t;
        }
        z = x->next;
        x->next = z->next;
        free(z);
        return h; }
    Hashtable.h:

    Code:
    #ifndef HASHTABLE_H#define HASHTABLE_H
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "LISTA.h"
    
    
    
    void Init(int M);
    Objective search(unsigned long id);
    void insert(Objective o);
    void delete(unsigned long id);
    Objectivenode insertBegin(Objectivenode h, pObjective obj);
    Objective searchList(Objectivenode h, unsigned long id);
    Objectivenode removeList(Objectivenode h, unsigned long id);
    
    
     #endif
    List.c:

    Code:
    #include <stdlib.h>#include <string.h>
    #include <stdio.h>
    #include "LISTA.h"
    
    
    
    Objectivenode insertEnd(Objectivenode head, lObjective obj){
    
    	Objectivenode t;
    	Objectivenode new = (Objectivenode)malloc(sizeof(struct Objectivenode));
    	new->obj = obj;
    	new->next = NULL;
    	if(head == NULL) return new;
    	for(t = head; t->next != NULL; t = t->next)
    		;
    	t->next = new;
    	return head;
    }
    
    int checkId(Objectivenode head, unsigned long id){
        Objectivenode t;
        int count = 0;
        for(t = head; t != NULL; t = t->next){
    		if(t->obj->id == id)
    			count++;
        }
        return count;
    }
    
    Objectivenode removeList2(Objectivenode head, unsigned long id){
    	Objectivenode t, x, z;
    	for(t = head; t != NULL; t = t->next){
    		if(t->next->obj->id == id)
    			x = t;
        }
        z = x->next;
        x->next = z->next;
        free(z);
        return head;
    }
    
    void print(Objectivenode head){
    	Objectivenode t;
    	int i;
    
    	for(t = head; t!=NULL; t = t->next){
    		printf("%lu %s %lu ", t->obj->id, t->obj->name, t->obj->duration);
    		for(i = 0; i < 9000; i++){
    			printf("%lu ",t->obj->deps[i]);
    		}
    		printf("\n");
    	}
    }
    List.h:

    Code:
    #ifndef LISTA_H#define LISTA_H
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Objective{
    	char name [8000];
    	unsigned long id, duration, deps [9000];
    	int hasDeps;
    }Objective;
    
    typedef struct Objectivenode{
       Objective *obj;
       struct Objectivenode *next;
    }Objectivenode;
    
    
    
    Objectivenode insertEnd(Objectivenode head, lObjective obj);
    int checkId(Objectivenode head, unsigned long id);
    Objectivenode removeList2(Objectivenode head, unsigned long id);
    void print(Objectivenode head);
     #endif
    The rest of the files are the same.
    I'm getting a ton of errors, i messed up somewhere and its really confusing, I don't really know where and how to change... can you help me?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-10-2017, 06:27 AM
  2. multiple definition of foo, first defined here
    By dayalsoap in forum C++ Programming
    Replies: 6
    Last Post: 09-04-2011, 11:12 AM
  3. Multiple definition error, one definition
    By frog in forum C++ Programming
    Replies: 9
    Last Post: 10-21-2010, 03:15 AM
  4. multiple definition??
    By coni in forum C Programming
    Replies: 4
    Last Post: 09-11-2009, 07:55 PM

Tags for this Thread