The problem here is that you are including commands.c in commands.h before the function prototype. Therefore, the C pre-processor inserts the content of commands.c into commands.h before the function prototype. commands.c contains the function definition. As a result, the function definition ends up before than the function declaration causing the error.
The content of commands.h after the pre-processor phase looks like this:
#ifndef COMMANDS_H_ #define COMMANDS_H_ // function definition void f123(){ } // function declaration void f123(); #endif /* COMMANDS_H_ */This is an error because you can't declare a function after its definition in C. If you swapped #include "commands.c" and the function declaration the error shouldn't happen because, now, the function prototype comes before the function declaration.
However, including a .c file is a bad practice and should be avoided. A better solution for this problem would be to include commands.h in commands.c and link the compiled version of command to the main file. For example:
commands.h
#ifndef COMMANDS_H_ #define COMMANDS_H_ void f123(); // function declaration #endifcommands.c
#include "commands.h" void f123(){} // function definition05-18-2018 #1
Registered User
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); #endifList.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); #endifObjective.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(); #endifMy 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 statusAnd i don't know where I'm wrong... can anyone help me?