1

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
12
  • 3
    Please indicate the exact command line parameters passed to the compiler. The compiler and linker need to be provided with a definition for matrix_product, not just a declaration, and to do this, the compiler must be passed a command line that includes matrix_product.c (or it must be compiled to a library and linked at a later step)
    – nanofarad
    May 8, 2020 at 22:28
  • 4
    It's not a problem with the code. It's a problem with the way you have set up your project. Probably you have not included matrix_product.c in your project.
    – kaylum
    May 8, 2020 at 22:29
  • @kaylum how can I include it?
    – TI84ES
    May 8, 2020 at 22:33
  • A good place to start would be the codeblocks manual. It tells you in detail how to add files to your project.
    – kaylum
    May 8, 2020 at 22:36
  • @TedLyngmo I wrote it like that in the actual code, that was a misprint on my part when I copied to the website, sorry.
    – TI84ES
    May 8, 2020 at 22:36

1 Answer 1

0

main.c seems to be the only source file added to the project in CodeBlocks. Add the source file matrix_product.c to the project by using Project->Add files and it'll compile that too and link (ld) with the object file it produces.

Just having the file in the same folder does not make CodeBlocks understand that it should include the file in the project.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.