I have a project I'm working on that currently has a matrix.c
file containing a some functions and has a corresponding header matrix.h
. I made a shared library libccalc.so
using matrix.c
and another file. I am trying to create a directory in /usr/local/lib
and a directory in /usr/local/include
, both called ccalc
to house the respective .so
and .h
files which can then later be used to compile programs using the functionality of libccalc.so
. However, when I do this I am getting an error.
To be precise, matrix.c
contains functions:
Matrix *mat_create(int rows, int cols)
,
void mat_fill(Matrix *mat, double *entries)
and
void mat_print(Matrix *mat)
which are declared in matrix.h
. I place the files in their respective directories as explained above, I run ldconfig /usr/local/lib/ccalc
and I make a new file test.c
in some other directory with the following:
// test.c
#include <stdio.h>
#include "matrix.h"
int main() {
Matrix *m = mat_create(2, 2);
double entries[4] = {1, 2, 3 ,4};
mat_fill(m, entries);
mat_print(m);
return 0;
}
matrix.h
contains the following:
// matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#define MAX_SIZE 50
typedef struct Matrix Matrix;
struct Matrix {
int rows;
int cols;
double *data;
};
Matrix *mat_create(int n, int p);
void mat_fill(Matrix *mat, double *entries);
void mat_print(Matrix *mat);
#endif
When I enter the command: gcc -I/usr/local/include/ccalc -L/usr/local/lib/ccalc -lccalc test.c -o test
, I get the error:
/usr/bin/ld: /tmp/ccesD44J.o: in function `main':
test.c:(.text+0x26): undefined reference to `mat_create'
/usr/bin/ld: test.c:(.text+0x71): undefined reference to `mat_fill'
/usr/bin/ld: test.c:(.text+0x7d): undefined reference to `mat_print'
collect2: error: ld returned 1 exit status
However, when I place libccalc.so
and matrix.h
in /usr/local/lib
and /usr/local/include
, enter ldconfig
and enter the command gcc -L/usr/local/lib/ccalc -lccalc test.c -o test
, and run ./test
, it works perfectly
Can someone tell me what I am doing wrong?