title: How to link all files in C ansi? Error with multiple definition
body: <p>Good morning</p>
  <p>This is big problem for me to link all files (<b>main.c, myFunc.c, myFuncHeaders.h</b> and <b>MakeFile</b>).
  Problem is <em>multiple definition</em> when I don't know what should I do with it. I used global variables <code>char board[3][3]</code>, <code>char board_checked[3][3]</code> and <code>int playersRound</code>. I know that global would be unnecessary and bad designed program, but I wanted try something new. Simple tic tac toe game</p>
  <p>In one file(all in one), code works. Here:</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  char board[3][3] = {{'_', '_', '_'},{'_', '_', '_'},{'_', '_', '_'} };
  char board_checked[3][3] = {{'+', '+', '+'},{'+', '+', '+'},{'+', '+', '+'}};
  int playersRound = 0;
  
  // It shows board
  void show_board(){
      char * space = &quot;          &quot;;
      puts(&quot;\tPlansza\n\t==================================&quot;);
      for(int i = 1; i &lt; 4; i++)
      {
          printf(&quot;%s|%d|&quot;, space, i);
      }
          printf(&quot;\n&quot;);
      for(int i = 0; i &lt; 3; i++){
          printf(&quot;\t|%d|&quot;,i+1);
          for(int j = 0; j &lt; 3; j++)
          {
              printf(&quot;%c  %s&quot;, board[i][j], space);
          }
          printf(&quot;\n\n&quot;);
      }
      puts(&quot;\t==================================\n&quot;);
  }
  
  
  // This function add a player's mark - 'X' or 'O' - to board
  void add_mark(int *p, int player){
      // If it isn't reserved by other mark
      if(board_checked[p[0]-1][p[1]-1] != '-'){
          // x (p[1]) and y (p[0]) vectors are (1, 2, 3)
          if(1 &lt;= p[0] &amp;&amp; p[0] &lt;= 3 &amp;&amp; 1 &lt;= p[1] &amp;&amp; p[1] &lt;= 3)
          {
              if(player == 1)
              {
                  // Add to board 'X'
                  board[p[0]-1][p[1]-1] = 'O';
                  // and unchecked at the same position 'board_checked'
                  board_checked[p[0]-1][p[1]-1] = '-';
  
              }
              else
              {
                  // Add to board 'X'
                  board[p[0]-1][p[1]-1] = 'X';
                  // and unchecked at the same position 'board_checked'
                  board_checked[p[0]-1][p[1]-1] = '-';
              }
              // If Everything is Okey go to next playersRound
              playersRound++;
          }
      }
      else
          // On start variable is position[2] = {0,0}; it's mean that I made exception
          if(!(0 == p[0] &amp;&amp; 0 == p[1]))
          {
              puts(&quot;Nie podales poprawnych parametrow&quot;);
              sleep(2);
          }
  }
  
  
  // This function check every position where any mark have been added
  int structure_check(int player){
      //return winner
      for(int i = 0; i &lt; 2; i++){
          // Check horizontal
          for(int level = 0; level &lt; 3; level++)
              if(board[level][0] == board[level][1] &amp;&amp; board[level][0] == board[level][2] &amp;&amp; board[level][0] != '_')return player;
          // Check vertical
          for(int level = 0; level &lt; 3; level++)
              if(board[0][level] == board[1][level] &amp;&amp; board[0][level] == board[2][level] &amp;&amp; board[0][level] != '_')return player;
          // Check diagonal left
          if(board[0][0] == board[1][1] &amp;&amp; board[0][0] == board[2][2] &amp;&amp; board[0][0] != '_')return player;
          // Check diagonal right
          if(board[2][0] == board[1][1] &amp;&amp; board[2][0] == board[0][2] &amp;&amp; board[0][2] != '_')return player;
      }
      // Draw - when no one of two players won
      return 0;
  }
  
  
  
  int main(char argc, char * argv[]){
      int player = 1;
      int position[2] = {0,0};
      int status = 0;
  
  
  
      show_board();
  
  
      while(playersRound &lt; 10){
          // Short information
          if(player == 1) puts(&quot;Gracz 1:&quot;);
          else puts(&quot;Gracz 2:&quot;);
          // Get position
          printf(&quot;\tx:&quot;); scanf(&quot;%d&quot;, &amp;position[0]);
          printf(&quot;\ty:&quot;); scanf(&quot;%d&quot;, &amp;position[1]);
          // status game, win or draw
          status = structure_check(player);
          // Players movement in playersRounds
          if(playersRound%2 == 0) player = 1;
          else player = 2;
          // This add mark to board by player
          add_mark(position, player);
          // Clear console
          system(&quot;cls&quot;);
          // Show only board
          show_board();
          // To be sure it works
          status = structure_check(player);
          // It finish when someone won game
          if(status != 0) break;
      }
  
      if(player == 1)
          printf(&quot;Wygrywa gracz: 1 z  'O'&quot;);
      else
          printf(&quot;Wygrywa gracz: 2 z  'X'&quot;);
      puts(&quot;  ||| Gratulacje! |||&quot;);
  
      return 0;
  }
  </code></pre>
  <p>Problem is here:</p>
  <p><strong>main.c</strong></p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt;
  #include &quot;myFunc.c&quot;
  #include &quot;myFuncHeaders.h&quot;
  
  
  
  int main(char argc, char * argv[]){
      int player = 1;
      int position[2] = {0,0};
      int status = 0;
      
      
  
      show_board();
      
  
      while(playersRound &lt; 10){
          // Short information
          if(player == 1) puts(&quot;Gracz 1:&quot;);
          else puts(&quot;Gracz 2:&quot;);
          // Get position
          printf(&quot;\tx:&quot;); scanf(&quot;%d&quot;, &amp;position[0]);
          printf(&quot;\ty:&quot;); scanf(&quot;%d&quot;, &amp;position[1]);
          // status game, win or draw
          status = structure_check(player);
          // Players movement in playersRounds
          if(playersRound%2 == 0) player = 1;
          else player = 2;
          // This add mark to board by player
          add_mark(position, player);
          // Clear console
          system(&quot;clear&quot;);
          // Show only board
          show_board();
          // To be sure it works
          status = structure_check(player);
          // It finish when someone won game
          if(status != 0) break;
      }
  
      if(player == 1)
          printf(&quot;Wygrywa gracz: 1 z  'O'&quot;);
      else
          printf(&quot;Wygrywa gracz: 2 z  'X'&quot;);
      puts(&quot;  ||| Gratulacje! |||&quot;);
  
      return 0;
  }
  </code></pre>
  <p><strong>myFunc.c</strong></p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;unistd.h&gt; // sleep() 
  #include &quot;myFuncHeaders.h&quot;
  
  char board[3][3] = {{'_', '_', '_'},{'_', '_', '_'},{'_', '_', '_'} };
  char board_checked[3][3] = {{'+', '+', '+'},{'+', '+', '+'},{'+', '+', '+'}};
  int playersRound = 0;
  
  // It shows board 
  void show_board(){
      char * space = &quot;          &quot;;
      puts(&quot;\tPlansza\n\t==================================&quot;);
      for(int i = 1; i &lt; 4; i++)
      {
          printf(&quot;%s|%d|&quot;, space, i);
      }
          printf(&quot;\n&quot;);
      for(int i = 0; i &lt; 3; i++){
          printf(&quot;\t|%d|&quot;,i+1);
          for(int j = 0; j &lt; 3; j++)
          {
              printf(&quot;%c  %s&quot;, board[i][j], space);
          }
          printf(&quot;\n\n&quot;);
      }
      puts(&quot;\t==================================\n&quot;);
  }
  
  
  // This function add a player's mark - 'X' or 'O' - to board
  void add_mark(int *p, int player){
      // If it isn't reserved by other mark
      if(board_checked[p[0]-1][p[1]-1] != '-'){
          // x (p[1]) and y (p[0]) vectors are (1, 2, 3)
          if(1 &lt;= p[0] &amp;&amp; p[0] &lt;= 3 &amp;&amp; 1 &lt;= p[1] &amp;&amp; p[1] &lt;= 3)
          {
              if(player == 1)
              {
                  // Add to board 'X'
                  board[p[0]-1][p[1]-1] = 'O';
                  // and unchecked at the same position 'board_checked'
                  board_checked[p[0]-1][p[1]-1] = '-';
  
              }
              else
              {
                  // Add to board 'X'
                  board[p[0]-1][p[1]-1] = 'X';
                  // and unchecked at the same position 'board_checked'
                  board_checked[p[0]-1][p[1]-1] = '-';
              }
              // If Everything is Okey go to next playersRound
              playersRound++;
          }
      }
      else
          // On start variable is position[2] = {0,0}; it's mean that I made exception
          if(!(0 == p[0] &amp;&amp; 0 == p[1]))
          {
              puts(&quot;Nie podales poprawnych parametrow&quot;);
              sleep(2);
          }
  }
  
  
  // This function check every position where any mark have been added
  int structure_check(int player){
      //return winner
      for(int i = 0; i &lt; 2; i++){
          // Check horizontal
          for(int level = 0; level &lt; 3; level++)
              if(board[level][0] == board[level][1] &amp;&amp; board[level][0] == board[level][2] &amp;&amp; board[level][0] != '_')return player;
          // Check vertical
          for(int level = 0; level &lt; 3; level++)
              if(board[0][level] == board[1][level] &amp;&amp; board[0][level] == board[2][level] &amp;&amp; board[0][level] != '_')return player;
          // Check diagonal left
          if(board[0][0] == board[1][1] &amp;&amp; board[0][0] == board[2][2] &amp;&amp; board[0][0] != '_')return player;
          // Check diagonal right
          if(board[2][0] == board[1][1] &amp;&amp; board[2][0] == board[0][2] &amp;&amp; board[0][2] != '_')return player;
      }
      // Draw - when no one of two players won
      return 0;
  }
  </code></pre>
  <p><strong>myFuncHeaders.h</strong></p>
  <pre><code>void show_board();
  // This function add a player's mark - 'X' or 'O' - to board
  void add_mark(int *p, int player);
  // This function check every position where any mark have been added
  int structure_check(int player);
  </code></pre>
  <p><strong>MakeFile</strong></p>
  <pre><code>main:               main.o myFunc.o
                      gcc -o main main.o myFunc.o
  
  myFunc.o:           myFunc.c myFunc.o
                      gcc -c -o myFunc.o myFunc.c
  </code></pre>
  <p><strong>Output:</strong></p>
  <pre><code>*my foleder*
  ~/zadania_domowe/kolko_krzyzyk $ make
  cc    -c -o main.o main.c
  make: circular version myFunc.o &lt;- myFunc.o lost.
  gcc -c -o myFunc.o myFunc.c
  gcc -o main main.o myFunc.o
  /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: myFunc.o:(.data+0x0): multiple definition of `board'; main.o:(.data+0x0): first defined here
  /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: myFunc.o:(.data+0x10): multiple definition of `board_checked'; main.o:(.data+0x10): first defined here
  /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: myFunc.o:(.bss+0x0): multiple definition of `playersRound'; main.o:(.bss+0x0): first defined here
  /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: myFunc.o: in function `show_board':
  myFunc.c:(.text+0x0): multiple definition of `show_board'; main.o:main.c:(.text+0x0): first defined here
  /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: myFunc.o: in function `add_mark':
  myFunc.c:(.text+0xf5): multiple definition of `add_mark'; main.o:main.c:(.text+0xf5): first defined here
  /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/bin/ld: myFunc.o: in function `structure_check':
  myFunc.c:(.text+0x2a6): multiple definition of `structure_check'; main.o:main.c:(.text+0x2a6): first defined here
  collect2: error: ld returned 1 exit status
  make: *** [makefile:2: main] Error 1
  </code></pre>
  <p>I try with <code>extern</code> global, put this globals into the <code>main()</code> and then send to the functions. I had more errors then I expected. If someone have link or source to the same problem I will be glad for this information.</p>