2015-11-25 46 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