cvitis

`conflicting types for 'matrix'` when C simulation in Vivado 2023


matrix.h as below:

#ifndef _MATRIX_H_
#define _MATRIX_H_
#include <stdio.h>
void matrix(char a,char b,char res);
#endif

matrix.c as below:

#include "matrix.h"
void matrix(
    char a[3][3],
    char b[3][3],
    char res[3][3]
)
{
int i=0,j=0,k=0;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
    res[i][j]=0;
    for(k=0;k<3;k++){
        res[i][j]+=a[i][k]*b[k][j];
    }
}
}
printf("%d \n",res);
}

matrix_test.c as below:

#include "matrix.h"
int main()
{
    char a[3][3]={
    {11,12,13},
    {14,15,16},
    {17,18,19}
};
    char b[3][3]={
    {21,22,23},
    {24,25,26},
    {27.28,29}
};
char res[3][3];
matrix(a,b,res);
}

It seems everything is ok,but I got error as ../../../../matrix.c:4:6: error: conflicting types for 'matrix'

Where is the problem?


Solution

  • The purpose of a header file (like “matrix.h” is to explain what things look like to other files using the code.

    So, for “matrix_test.c” to be able to use the code in “matrix.c”, it has to know what the code in “matrix.c” looks like.

    The easiest way to do that is to simply copy the function header from “matrix.c” and paste it into “matrix.h”.

    #ifndef _MATRIX_H_
    #define _MATRIX_H_
    
    void matrix(
        char a[3][3],
        char b[3][3],
        char res[3][3]
    );
    
    #endif
    

    Remember, a char is NOT the same as a char[M][N]. One is a single character, the other is an array.

    Also, avoid sticking includes in header files that are not necessary. Only put includes in files that use stuff that require it. So, stick the #include <stdio.h> in “matrix.c”, since that is where it is used.