Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. multiple definition of.....

multiple definition of.....

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krish_kumar
    wrote on last edited by
    #1

    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

    C K A 3 Replies Last reply
    0
    • K krish_kumar

      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

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      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]

      1 Reply Last reply
      0
      • K krish_kumar

        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

        K Offline
        K Offline
        KingsGambit
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • K KingsGambit

          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

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          That (probably) will not solve his problem, since (apparently) the header file is included both by display.c and create.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]

          K 1 Reply Last reply
          0
          • C CPallini

            That (probably) will not solve his problem, since (apparently) the header file is included both by display.c and create.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]

            K Offline
            K Offline
            KingsGambit
            wrote on last edited by
            #5

            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.

            D 1 Reply Last reply
            0
            • K krish_kumar

              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

              A Offline
              A Offline
              Avi Berger
              wrote on last edited by
              #6

              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. Your node_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'

              1 Reply Last reply
              0
              • K KingsGambit

                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.

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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

                K 1 Reply Last reply
                0
                • D David Crow

                  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

                  K Offline
                  K Offline
                  KingsGambit
                  wrote on last edited by
                  #8

                  Thanks for correcting. I would have mentioned "defining". :)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups