海合会 "undefined reference to"
gcc "undefined reference to"
我无法找到我的程序无法编译的原因。我不太擅长 C,所以我希望有人能发现我的错误。有人告诉我这可能是一个 header 问题,这就是我从这个开始的原因。这是消息:
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
这是我的文件,但为了简洁起见,我将只包括提到的函数声明。
设置如下:
生成文件
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
test: polynomial.o counting.o matrix.o
gcc -Wall -Werror test.c -o test polynomial.o counting.o -g -std=c99
应该是
test: polynomial.o counting.o matrix.o
gcc -Wall -Werror test.c -o test polynomial.o counting.o matrix.o -g -std=c99
这是一个 linker 错误。当 linker 找不到您要求它 link.
的已编译函数时,就会发生这些类型的错误
如果您查看 matrix.h 文件,您会发现以下声明
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);
这基本上告诉编译器'假设这些函数存在,我会确保你在编译可执行文件时得到它们'
我不知道这些函数是从哪里来的,也不知道它们有什么用,但是你必须将包含它们的目标文件传递给 linker。
我无法找到我的程序无法编译的原因。我不太擅长 C,所以我希望有人能发现我的错误。有人告诉我这可能是一个 header 问题,这就是我从这个开始的原因。这是消息:
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
这是我的文件,但为了简洁起见,我将只包括提到的函数声明。
设置如下:
生成文件
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
test: polynomial.o counting.o matrix.o
gcc -Wall -Werror test.c -o test polynomial.o counting.o -g -std=c99
应该是
test: polynomial.o counting.o matrix.o
gcc -Wall -Werror test.c -o test polynomial.o counting.o matrix.o -g -std=c99
这是一个 linker 错误。当 linker 找不到您要求它 link.
的已编译函数时,就会发生这些类型的错误如果您查看 matrix.h 文件,您会发现以下声明
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);
这基本上告诉编译器'假设这些函数存在,我会确保你在编译可执行文件时得到它们'
我不知道这些函数是从哪里来的,也不知道它们有什么用,但是你必须将包含它们的目标文件传递给 linker。