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
TI84ES
  • 25
  • 5
  • 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 08 '20 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 08 '20 at 22:29
  • @kaylum how can I include it? – TI84ES May 08 '20 at 22:33
  • A good place to start would be the [codeblocks manual](http://www.codeblocks.org/docs/manual_codeblocks_en.pdf). It tells you in detail how to add files to your project. – kaylum May 08 '20 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 08 '20 at 22:36
  • @anastaciu I haven't defined it, I was trying to pass a value corresponding to the dimension of the matrix as a parameter to the matrix_product function. Where should I define it? – TI84ES May 08 '20 at 22:50
  • @TI84ES, yes sorry, read diagonally. – anastaciu May 08 '20 at 22:52
  • `main()` is not a valid signature for anything but a default constructor in a class called `main`. – Ted Lyngmo May 08 '20 at 22:57
  • @TedLyngmo Ok, sorry man, I'm not a programmer, I'm just trying to learn some things for my college's computer science class. – TI84ES May 08 '20 at 23:10
  • @TedLyngmo That's what I typed, and it produces the executable file which works. But I'm not being able to build it in CodeBlocks 17.12 – TI84ES May 08 '20 at 23:13
  • @TI84ES everything is fine with the code, you could include stdio.h and stdlib only in the header file but those are details, something in code blocks config is not right, or you didn't set up the project correctly, I found this 9 year old thread, though it's C++, the problem looks similar, see if it helps https://stackoverflow.com/q/5971206/6865932 – anastaciu May 08 '20 at 23:19
  • @anastaciu Thank you! but Ted Lyngmo already figured it out :) – TI84ES May 08 '20 at 23:24

1 Answers1

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.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108