Using gcc -c to generate .o files -
so had create bit of code create list , various things it. print it, sort it, , see if value in it. did that, ran fine. i've got take functions , split them separate files, utilize gcc -c (which i'm not super sure i'm using correctly) .o files, plus .o test program. have utilize gcc link .o files executable. prompt says recognize .o's , realize how link them.
so here questions: why below code returning errors( in ways defined below)? , supposed writing command line link these guys?
so code follows: (first .h files, main .c file)
node.h
typedef struct node{ int data; struct node *next; struct node *prev; }node;
print.h
#include<stdio.h> #include"node.h" void print(node *pointer){ if (pointer == null){ return; } printf("%d ",pointer->data); print(pointer->next); }
init.h
#include<stdio.h> #include"node.h" int init(node *pointer,int find){ pointer = pointer->next; while (pointer != null){ if (pointer->data == find)//found find { printf("the info in list."); homecoming 1; } pointer = pointer->next;// search in next node. } //find not found printf("the info not in list."); homecoming 0; }
sort.h
#include<stdio.h> #include"node.h" void swap (node *x, node *y){ int temp = x->data; x->data = y->data; y->data = temp; } void sort(node*pointer){ int i; while (pointer->next != null){ if (pointer->data>pointer->next->data){ swap(pointer,pointer->next); } pointer = pointer->next; sort(pointer); } }
list.c
#include<stdio.h> #include<stdlib.h> #include"node.h" #lnclude"print.h" #include"sort.h" #include"init.h" int i; node *p; node *n; void insert(node *pointer, int data){ //go through list till ya find lastly node while (pointer->next != null){ pointer = pointer->next; } //allocate memory new node , set info in pointer->next = (node *)malloc(sizeof(node)); (pointer->next)->prev = pointer; pointer = pointer->next; pointer->data = data; pointer->next = null; } int main(){ //start used point first node //temp lastly node node *start, *temp; int z; start = (node *)malloc(sizeof(node)); temp = new; temp->next = null; temp->prev = null; (z = 0; z < 10; z++){ insert(start,(3*10) - z); } init(start,12); init(start,3); init(start,27); init(start,7); print(start); sort(start); print(start); }
now code ran fine when together, exception of node.h (that separate file). .h files compile perfectly, when seek compile .c file returns errors claiming trying redefine node in each .h file. because including in each .h file?
i getting errors trying pass inappropriate arguments init function, not seem case. might on looking things.
thank in advance help.
edit: changed variable in main new start errors when typing "gcc list.c"
in file included init.h:2:0, list.c:4: node.h:1:16 error: redefinition of'struct node' node.h:1:16 note: defined here node.h:5:2 error: conflicting types 'node' node.h:5:2 note: previous declaration of 'node' here in file included sort.h:2:0; list.c:5: node.h:1:16 error: redefinition of'struct node' node.h:1:16 note: defined here node.h:5:2 error: conflicting types 'node' node.h:5:2 note: previous declaration of 'node' here list.c: in function 'main': list.c:41:1: warning: passing argument 1 of 'init' incompatible pointer type[enabled default] init.h:4:5: note: expected 'struct node *' argument of type 'struct node *'
(and there errors each of separate init calls in main)
list.c:45:1: warning:passing argument 1 of 'print' incompatible pointer type [enabled default] print.h:3:6: note expected 'struct node *' argument of type 'struct node *'
(and there 1 sec print function)
put inclusion barriers in .h files. eg, sort.h
#ifndef include_sort_h #define include_sort_h // contents of file #endif
edit
actually looked @ code more closely. have defined functions in .h files not thing do. see no reason separate separate files @ all.
so combine them single file , compile with:
gcc -o list -wall list.c
if want separate files, set functions c files , construction , prototypes .h file (which include each c file). compile , link using like:
gcc -o list -wall list.c node.c main.c
c gcc linked-list
No comments:
Post a Comment