I recently looked into defining functions using a header file in C. I followed a tutorial online, but I have come across an issue: If I use command prompt and run the executable file I created in my project folder, it takes the input from my main.c file and passes it through the function like I expected it to.
The command that I typed in Command Prompt was:
gcc matrix_product.c main.c
Within main I can call the function (which I named matrix_product), and it recognizes it. When I try to build it inside CodeBlocks, however, the compiler indicates 2 errors:
undefined reference to 'matrix_product'
error: ld returned 1 exit status
This is the code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include "matrix_product.h"
#define N 3
int main()
{
int i,j;
int m[N][N]={
{1,2,3},
{4,5,6},
{7,8,9}
};
matrix_product(m,3);
for(i=0;i<N;i++){
if(i==1){printf("M^2 = ");
}else{printf(" ");}
for(j=0;j<N;j++){
printf("%d ",m[i][j]);
}
printf("\n");
}
return 0;
}
matrix_product.c
#include <stdio.h>
#include "matrix_product.h"
void matrix_product(int m[][3],int DIM)
{
int i,j,k;
int tmp[DIM][DIM];
for(i=0;i<DIM;i++){
for(j=0;j<DIM;j++){
tmp[i][j]=m[i][j];
m[i][j]=0;
}
}
for(i=0;i<DIM;i++){
for(j=0;j<DIM;j++){
for(k=0;k<DIM;k++){
m[i][j]+=tmp[i][k]*tmp[k][j];
}
}
}
}
matrix_product.h
#ifndef MATPROD
#define MATPROD
void matrix_product(int m[][3],int DIM);
#endif // MATPROD