Search code examples
ccompiler-errorsdefinitionlinkage

Trouble with C & Code Blocks : Error with debug called 'error: ld returned 1 exit status'


I past all my day to fix Code Blocks, I had a lot of troubles with it. Seems to be fixed, I decide to code and i'm trying to display a tray ( two dimensional arrays ) as a parameter of a function. I follow an answer on this website to make it right. But now, i have an error when I compile. Here's my files.

main.c

#include <stdio.h>
#include <stdlib.h>
#include "SudokuH.h"

int main(void)
{
    int tray[9][9]={};
    displayTray(numRows, numCols, tray);
    return 0;
}

SudokuH.h

#ifndef SUDOKUH_H_INCLUDED
#define SUDOKUH_H_INCLUDED

int numRows = 9;
int numCols = 9;
int i,j;

void displayTray (int numRows, int numCols, int pt[][numCols]);

#endif // SUDOKUH_H_INCLUDED

SudokuS.c

#include <stdio.h>
#include <stdlib.h>
#include "SudokuH.h"

void displayTray(int numRows, int numCols, int pt[][numCols]){
    printf("A|B|C|D|E|F|G|H|I\n");
    for (i=0; i<numRows;i++){
            printf("%d|",i);
            for (j=0; j<numCols;j++){
                printf("%i|",pt[i][j]);
            }
    }
}

At the beginning, I thought this error came from CodeBlocks but i try to make again without creating a project and it didn't work. And my others programs seem to work. So what's wrong with my code ? I checked my parameter but it seems ok so maybe it's my way to use a two dimensional array as parameter ? The error is :

||=== Build: Debug in Sudoku (compiler: GNU GCC Compiler) ===| ||error: ld returned 1 exit status| ||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

I have this in the build log :

obj\Debug\main.o:main.c:(.data+0x0): first defined here obj\Debug\SudokuS.o:SudokuS.c:(.data+0x4): multiple definition of `numCols' obj\Debug\main.o:main.c:(.data+0x4): first defined here collect2.exe: error: ld returned 1 exit status


Solution

  • the following code:

    1. has no 'magic' numbers
    2. in the display() function, fixes the display of the 'header' line
    3. drastically reduces the number of parameters to the display() function
    4. cleanly compiles
    5. uses unsigned variables when handling numbers that will only be >= 0
    6. stuffed everything in one file, for brevity
    7. does not define variable instances in the header file
    8. does not #include header files that are not used
    9. properly initializes the tray[][] array
    10. corrects the display() function to output lines separated by a newline

    and now the code

    #include <stdio.h>
    
    //SudokuH.h
    
    #ifndef SUDOKUH_H_INCLUDED
    #define SUDOKUH_H_INCLUDED
    
    #define NUM_ROWS (9)
    #define NUM_COLS (9)
    
    void displayTray (int pt[][ NUM_COLS ]);
    
    #endif // SUDOKUH_H_INCLUDED
    //#include "SudokuH.h"
    
    int main(void)
    {
        int tray[ NUM_ROWS ][ NUM_COLS ]={{ 0 }};
        displayTray(tray);
        return 0;
    }
    
    
    
    //SudokuS.c
    
    #include <stdio.h>
    //#include "SudokuH.h"
    
    void displayTray(int pt[][ NUM_COLS ])
    {
        printf(" |A|B|C|D|E|F|G|H|I|\n");
    
        for (size_t i=0; i<NUM_ROWS; i++)
        {
            printf("%lu|",i);
            for (size_t j=0; j<NUM_COLS; j++)
            {
                printf("%i|", pt[i][j]);
            }
            printf( "\n");
        }
    }
    

    the output looks like:

     |A|B|C|D|E|F|G|H|I|
    0|0|0|0|0|0|0|0|0|0|
    1|0|0|0|0|0|0|0|0|0|
    2|0|0|0|0|0|0|0|0|0|
    3|0|0|0|0|0|0|0|0|0|
    4|0|0|0|0|0|0|0|0|0|
    5|0|0|0|0|0|0|0|0|0|
    6|0|0|0|0|0|0|0|0|0|
    7|0|0|0|0|0|0|0|0|0|
    8|0|0|0|0|0|0|0|0|0|