multiple definition of.....
-
hii Here I give my header file and .c file /* main.h*/ #include #include struct linked_list{ int data; struct linked_list *next; }; typedef struct linked_list node_t; void display (void); node_t *root = 0; ---------------------------------------------------------------------- /* display.c */ #include "./../inc/main.h" extern node_t *root; void display ( ) { node_t *temp = root; while (temp -> next != root) printf(" %d", temp->data); } ---------------------------------------------------------------------------- while compiling it gives this error qs2/src/display.c:8: multiple definition of `root' obj/create.o:/qs2/src/create.c:6: first defined here obj/display.o: In function `display': I think this error reltes to node_t *root = 0; ,extern node_t *root; but I dont know how to clear it ...??? seeking help ...!! Thnaking you
-
hii Here I give my header file and .c file /* main.h*/ #include #include struct linked_list{ int data; struct linked_list *next; }; typedef struct linked_list node_t; void display (void); node_t *root = 0; ---------------------------------------------------------------------- /* display.c */ #include "./../inc/main.h" extern node_t *root; void display ( ) { node_t *temp = root; while (temp -> next != root) printf(" %d", temp->data); } ---------------------------------------------------------------------------- while compiling it gives this error qs2/src/display.c:8: multiple definition of `root' obj/create.o:/qs2/src/create.c:6: first defined here obj/display.o: In function `display': I think this error reltes to node_t *root = 0; ,extern node_t *root; but I dont know how to clear it ...??? seeking help ...!! Thnaking you
krish_kumar wrote:
node_t *root = 0;
You must NOT initialize the global variable inside the header (
.h
) file, change it to:node_t *root;
Then inside just one source (
.c
) file (e.g.create.c
) initialize it this way:node_t *root = 0;
:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
hii Here I give my header file and .c file /* main.h*/ #include #include struct linked_list{ int data; struct linked_list *next; }; typedef struct linked_list node_t; void display (void); node_t *root = 0; ---------------------------------------------------------------------- /* display.c */ #include "./../inc/main.h" extern node_t *root; void display ( ) { node_t *temp = root; while (temp -> next != root) printf(" %d", temp->data); } ---------------------------------------------------------------------------- while compiling it gives this error qs2/src/display.c:8: multiple definition of `root' obj/create.o:/qs2/src/create.c:6: first defined here obj/display.o: In function `display': I think this error reltes to node_t *root = 0; ,extern node_t *root; but I dont know how to clear it ...??? seeking help ...!! Thnaking you
If root is already declared inside the main.h file and that header is included in the display.c, then why the extern node_t *root; is declared in display.c? I think you can remove extern node_t *root; from display.c
-
If root is already declared inside the main.h file and that header is included in the display.c, then why the extern node_t *root; is declared in display.c? I think you can remove extern node_t *root; from display.c
That (probably) will not solve his problem, since (apparently) the header file is included both by
display.c
andcreate.c
(incidentally this shows why you should never define a variable inside a header file). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
That (probably) will not solve his problem, since (apparently) the header file is included both by
display.c
andcreate.c
(incidentally this shows why you should never define a variable inside a header file). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Yes, it is better not to declare variable inside the headers. I was pointing out, when the header is included in the c file, it become a single translation unit. So, the unit already has the variable. Then there is no extern is required.
-
hii Here I give my header file and .c file /* main.h*/ #include #include struct linked_list{ int data; struct linked_list *next; }; typedef struct linked_list node_t; void display (void); node_t *root = 0; ---------------------------------------------------------------------- /* display.c */ #include "./../inc/main.h" extern node_t *root; void display ( ) { node_t *temp = root; while (temp -> next != root) printf(" %d", temp->data); } ---------------------------------------------------------------------------- while compiling it gives this error qs2/src/display.c:8: multiple definition of `root' obj/create.o:/qs2/src/create.c:6: first defined here obj/display.o: In function `display': I think this error reltes to node_t *root = 0; ,extern node_t *root; but I dont know how to clear it ...??? seeking help ...!! Thnaking you
You have it backwards. Your
extern node_t *root;
belongs in your .h file. It declares to anyone that includes it that there is such a variable that is created someplace and can be used. Yournode_t *root = 0;
belongs in exactly one .c file and no .h file. It defines (creates) the variable. You only do this once. If you put this in a header file, than each .c file that uses that header would create a separate variable of that name. Those multiple variables of the same name which are globally visible and accessible would conflict with each other, hence the error multiple definition of `root' -
Yes, it is better not to declare variable inside the headers. I was pointing out, when the header is included in the c file, it become a single translation unit. So, the unit already has the variable. Then there is no extern is required.
Rejeesh.T.S wrote:
Yes, it is better not to declare variable inside the headers.
I think you are confusing declaring with defining.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
Rejeesh.T.S wrote:
Yes, it is better not to declare variable inside the headers.
I think you are confusing declaring with defining.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
Thanks for correcting. I would have mentioned "defining". :)