cgccc-header

gcc "undefined reference to"


I'm having trouble finding why my program wont compile. Im not very good at C so I hope someone can spot my error. I was told it was probably a header issue, that is why I'm starting with this. This is the message:

carson@carson-Q303UA:~/Desktop/Github/ccomputing$ make
gcc -Wall -Werror test.c -o test polynomial.o counting.o -g -std=c99
/tmp/ccfRUFXx.o: In function `main':
/home/carson/Desktop/Github/ccomputing/test.c:40: undefined reference to `pymat_create'
/home/carson/Desktop/Github/ccomputing/test.c:41: undefined reference to `pymat_create'
/home/carson/Desktop/Github/ccomputing/test.c:43: undefined reference to `pymat_set_element'
/home/carson/Desktop/Github/ccomputing/test.c:44: undefined reference to `pymat_set_element'
/home/carson/Desktop/Github/ccomputing/test.c:45: undefined reference to `pymat_set_element'
/home/carson/Desktop/Github/ccomputing/test.c:46: undefined reference to `pymat_set_element'
/home/carson/Desktop/Github/ccomputing/test.c:48: undefined reference to `pymat_sum'
/home/carson/Desktop/Github/ccomputing/test.c:52: undefined reference to `pymat_get_element'
/home/carson/Desktop/Github/ccomputing/test.c:59: undefined reference to `pymat_delete'
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'test' failed
make: *** [test] Error 1

Here are my files, but for brevity, I will only include the function declarations mentioned.

So here is the setup:

Makefile

all: test

test: polynomial.o counting.o matrix.o
    gcc -Wall -Werror test.c -o test polynomial.o counting.o -g -std=c99

matrix.o: matrix.c 
    gcc -c matrix.c

polynomial.o: polynomial.c
    gcc -c polynomial.c

counting.o: counting.c
    gcc -c counting.c

test.c

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
#include <assert.h>
#include "array.h"
#include "matrix.h"
#include "polynomial.h"


int main() {

polynomial.h

// polynomial
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

typedef struct Polynomial Polynomial;

struct Polynomial {
    int deg;
    double *coefs;
};
// some functions

#endif

polynomial.c

#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
#include <assert.h>
#include "array.h"
#include "counting.h"
#include "matrix.h"
#include "polynomial.h"

#define TRUE 1
#define FALSE 0

matrix.h

#ifndef MATRIX_H
#define MATRIX_H

#include "polynomial.h"

typedef struct Matrix Matrix;
typedef struct PolyMatrix PolyMatrix;

struct Matrix {
    int rows;
    int cols;
    double *data;
};

struct PolyMatrix {
    int rows;
    int cols;
    Polynomial *data;
};

PolyMatrix pymat_create(int rows, int cols);
void pymat_clear(PolyMatrix A);
void pymat_delete(PolyMatrix mat);
PolyMatrix pymat_zero(int rows, int cols);
Polynomial pymat_get_element(PolyMatrix mat, int row, int col);
void pymat_set_element(PolyMatrix mat, int row, int col, Polynomial element);
PolyMatrix pymat_get_rows(PolyMatrix mat, int rows, int *rows_arr);
PolyMatrix pymat_get_cols(PolyMatrix mat, int cols, int *cols_arr);
PolyMatrix pymat_join(PolyMatrix A, PolyMatrix B, int axis);
//void pymat_print(PolyMatrix mat);
PolyMatrix pymat_copy(PolyMatrix mat);



#endif

matrix.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "array.h"
#include "polynomial.c"
#include "matrix.h"

#define TRUE 1
#define FALSE 0

Solution

  • test: polynomial.o counting.o matrix.o
        gcc -Wall -Werror test.c -o test polynomial.o counting.o -g -std=c99
    

    should be

    test: polynomial.o counting.o matrix.o
        gcc -Wall -Werror test.c -o test polynomial.o counting.o matrix.o -g -std=c99