2015-11-25 26 views
-3

我想写一个需要两个本地库的C程序,让我们说库1和库2需要库1,所以我包括头的库2的头部中的库1。但是,我收到一些错误。他们都是这样的:未定义的引用“功能名”,其中“函数名称”是库1的函数,下面是我的文件:C - 错误:未定义的引用'函数名'

complex.h:

typedef long double VALUE; 

typedef struct complex { 
    VALUE x; 
    VALUE y; 
} COMPLEX; 

void set_complex(COMPLEX *, VALUE, VALUE); 
COMPLEX mult2(COMPLEX *, COMPLEX *); 
COMPLEX square(COMPLEX *); 
COMPLEX add2(COMPLEX *,COMPLEX *); 
COMPLEX juliamap(COMPLEX *, COMPLEX *); 
void complex_print(COMPLEX *); 

工作平面。 H:

#include "complex.h" 

typedef unsigned long int INT; 

typedef struct cplane { 
    INT rows; 
    INT cols; 
    COMPLEX *mat; 
} CPLANE; 

CPLANE new_cplane(const INT, const INT); 
void delete_cplane(CPLANE); 
COMPLEX get(const CPLANE *, const INT, const INT); 
void set(CPLANE *, const VALUE, const VALUE, const VALUE, const VALUE, const INT, const INT); 
CPLANE constructor(const VALUE, const VALUE, const VALUE, const VALUE, const INT, const INT); 
void print_cplane(const CPLANE *); 

complex.c里:

#include<stdio.h> 
#include"complex.h" 
void set_complex(COMPLEX *c, VALUE a, VALUE b){ 
    c->x=a; 
    c->y=b; 
} 

COMPLEX mult2(COMPLEX *a, COMPLEX *b){ 
    COMPLEX z; 
    z.x=(a->x)*(b->x)-(a->y)*(b->y); 
    z.y=(a->x)*(b->y)+(a->y)*(b->x); 
    return z; 
} 

COMPLEX square(COMPLEX *a){ 
    return mult2(a,a); 
} 

COMPLEX add2(COMPLEX *a, COMPLEX *b){ 
    COMPLEX z; 
    z.x=(a->x)+(b->x); 
    z.y=(a->y)+(b->y); 
    return z; 
} 

COMPLEX juliamap(COMPLEX *a, COMPLEX *b){ 
    COMPLEX c = square(a); 
    return add2(&c,b); 
} 

void complex_print(COMPLEX *a){ 
    printf("%Lf+%Lfi",a->x,a->y); 
} 

cplane.c:

#include <stdio.h> 
#include <stdlib.h> 
#include "cplane.h" 

CPLANE new_cplane(const INT xp, const INT yp){ 
    CPLANE c; 
    c.rows = yp; 
    c.cols = xp; 
    c.mat = (COMPLEX *)calloc(yp * xp, sizeof(COMPLEX)); 
    if (c.mat == NULL) { 
    fprintf(stderr, "ERROR: failed to allocate new_matrix\n"); 
    } 
    return c; 
} 

void delete_cplane(CPLANE c){ 
    free(c.mat); 
} 

COMPLEX get(const CPLANE *c, const INT row, const INT col){ 
    if (row < 0 || col < 0 || row >= c->rows || col >= c->cols) { 
    fprintf(stderr, "ERROR: indexing cplane outside bounds"); 
    COMPLEX a; 
    set_complex(&a,0,0); 
    return a; 
    } 
    return *(c->mat + c->cols * row + col); 
} 

void set(CPLANE *c, const VALUE xmin, const VALUE xmax, const VALUE ymin, const VALUE ymax, const INT xpoints, const INT ypoints){ 
    const VALUE xstep = (xmax - xmin)/(xpoints - 1); 
    const VALUE ystep = (ymax - ymin)/(ypoints - 1); 
    INT i, j; 
    for (i=0; i<ypoints; i++){ 
    for (j=0; j<xpoints; j++){ 
     set_complex(c->mat + (c->cols * i) + j, xmin + j * xstep, ymin + i * ystep); 
    } 
    } 
} 

CPLANE constructor(const VALUE xmin, const VALUE xmax, const VALUE ymin, const VALUE ymax, const INT xpoints, const INT ypoints){ 
    CPLANE c = new_cplane(xpoints,ypoints); 
    if (xpoints < 2 || ypoints < 2) { 
    fprintf(stderr, "ERROR: xpoints and ypoints should be at least equal to 2"); 
    } 
    else if (xmin >= xmax){ 
    fprintf(stderr, "ERROR: xmax has to be greater than xmin"); 
    } 
    else if (ymin >= ymax){ 
    fprintf(stderr, "ERROR: ymax has to be greater than ymin"); 
    } 
    else{ 
    set(&c,xmin, xmax, ymin, ymax, xpoints, ypoints); 
    } 
    return c; 
} 

void print_cplane(const CPLANE *cp){ 
    INT i, j; 
    printf("Cplane (rows: %ld, cols: %ld) \n", cp->rows, cp->cols); 
    for(i=0; i<(cp->rows); i++) { 
    for(j=0; j<(cp->cols); j++) { 
     COMPLEX z = get(cp, i, j); 
     complex_print(&z); 
     printf(" "); 
    } 
    puts(""); 
    } 
    puts(""); 
} 

test_cplane.c:

#include <stdio.h> 
#include "cplane.h" 

int main(void) { 
    CPLANE a = constructor(3.2,6.2,2.0,4.0,10,10); 
    print_cplane(&a); 
    delete_cplane(a); 
    return 0; 
} 

的Makefile:

all: test_cplane 
test_cplane: cplane.o 
run: all 
    ./test_cplane 
clean: 
    rm -f test_cplane cplane.o 

什么是错我的代码?

+1

看一看生成日志。特别是链接'test_cplane'的行。我想你会发现它没有链接'cplane.o'和'complex.o'。 – kaylum

+0

谢谢!你的评论确实帮了我。这是我的第一个C代码之一......我只需要在我的Makefile中添加complex.o。 –

回答

0

用户kaylum帮助了我。

我只需要在Makefile中做一些改动,添加complex.o。

文件的修正版本是:

all: test_cplane 
test_cplane: cplane.o complex.o 
run: all 
    ./test_cplane 
clean: 
    rm -f test_cplane cplane.o complex.o