c - 链接器错误 undefined reference ... c 中包含的库

标签 c reference linker undefined

我为此搜索了很多,但我真的想不通。 我的项目包含 5 个 .c 文件和 4 个 .h header 。都在同一个文件夹中。 我分别成功地编译了它们但是当涉及到组编译时(gcc -o progname 和 linux 环境中的所有代码文件或 dev-c++ 中的编译所有选项)我在这两种情况下都有这些错误:

 C:\Users\user\...\main.o(.text+0x9a) In function `main': 
   [Linker error] undefined reference to `dhmiourgia_Words'
   [Linker error] undefined reference to `katastrofh_Words'
 C:\Users\user\...\main.o(.text+0x1fb) In function `InitialiseTree':
   [Linker error] undefined reference to `InsertWord' 
   ....

它继续无法识别我在 main.c 中使用的所有函数,在 Words.h 中定义并在 Words.c 中实现

我最初以为我错过了 header #include 预处理器命令,但正如您在这里看到的那样! 我怎样才能克服这些错误? 下面是问题所在的代码文件,欢迎大家指教,再次感谢

/* file: main.c */

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>                /* for gettimeofday() */
#include "typos_stoixeiouDDA.h"
#include "Words.h"


void InitialiseTree(FILE *wordlist, typosWords W, int totalwordsin);
void SearchUpdateTree(FILE *wordlist, typosWords W);
void Results(typosWords W, int totalwordsin);


int main(void)
{  typosWords A;
   FILE *FirstList, *SecondList;
   char ap;
   char ylop='B';
   char arxeio[25];
   int totalwordsin=0;

    printf("Den exei dhmiourgithei ATD-Words\nDhmiourgia? (n/N gia epivevaiwsh)\n");
    fflush(stdin);
    ap=getchar();
    if((ap=='n')||(ap=='N')){
        do{
            printf("Epilekste ylopoihsh dendrou (A gia AVL, B gia BST)\n");
            fflush(stdin);
            ylop=getchar();
            if((ylop=='B')||(ylop=='A')){
                A=dhmiourgia_Words();

                printf("Dwse onoma arxeiou-listas leksewn pros anazithsh(ston idio ypofakelo!)\n");
                scanf(" %s ", arxeio);
                if((FirstList=fopen(arxeio, "r"))==NULL)
                    printf("Sfalma kata to anoigma tou arxeiou\n");
                InitialiseTree(FirstList, A, totalwordsin);
                fclose(FirstList);

                printf("Arxeio pros eksetash: RomeoAndJuliet.txt\n");
                if((SecondList=fopen("RomeAndJuliet.txt", "r"))==NULL)
                    printf("Sfalma kata to anoigma tou arxeiou\n");
                SearchUpdateTree(SecondList, A);
                fclose(SecondList);

                Results(A, totalwordsin);
                katastrofh_Words(&A);
            }
            else printf("Lathos apantisi\n");
        }while((ylop!='A')&&(ylop!='B'));
    }
    printf("Eyxaristoyme poy mas protimhsate!\n");
    return 0;
}

void InitialiseTree(FILE *wordlist, typosWords W, int totalwordsin)
{
    int i=0, res;
    char word[20];
    struct timeval start, t0, t1, t2, t3, t4, t5, t6, tALL;
    double elapsedTime;

    gettimeofday(&start, NULL);    /* start timer */
    do{
        res=fscanf(wordlist,"%s\n", word);
        InsertWord(W, word);
        i++;
        if(i==1024*(2^0))
            gettimeofday(&t0, NULL);    /* save time */
        if(i==1024*(2^1))
            gettimeofday(&t1, NULL);    /* save time */
        if(i==1024*(2^2))
            gettimeofday(&t2, NULL);    /* save time */
        if(i==1024*(2^3))
            gettimeofday(&t3, NULL);    /* save time */
        if(i==1024*(2^4))
            gettimeofday(&t4, NULL);    /* save time */
        if(i==1024*(2^5))
            gettimeofday(&t5, NULL);    /* save time */
        if(i==1024*(2^6))
            gettimeofday(&t6, NULL);    /* save time */
    }while(res!=EOF);

    totalwordsin=i;
    gettimeofday(&tALL, NULL);          /* save total time */

    /* calculating time for 1024*2^0 words to be inserted */
    elapsedTime = (t0.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t0.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 0);

    /* calculating time for 1024*2^1 words to be inserted */
    elapsedTime = (t1.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t1.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 1);

    /* calculating time for 1024*2^2 words to be inserted */
    elapsedTime = (t2.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t2.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 2);

    /* calculating time for 1024*2^3 words to be inserted */
    elapsedTime = (t3.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t3.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 3);

    /* calculating time for 1024*2^4 words to be inserted */
    elapsedTime = (t4.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t4.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 4);

    /* calculating time for 1024*2^5 words to be inserted */
    elapsedTime = (t5.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t5.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 5);

    /* calculating time for 1024*2^6 words to be inserted */
    elapsedTime = (t6.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (t6.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 6);

    /* calculating time for all words to be inserted */
    elapsedTime = (tALL.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (tALL.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetInsertTime(W, elapsedTime, 7);

    /* Reads words from wordlist(1)
       inserts into DDA/AVL using InsertWord after 1024, 2048, 4096,... words
       sets array times using SetInsertTime */
}

void SearchUpdateTree(FILE *wordlist, typosWords W)
{
    struct timeval start, end;
    double elapsedTime;
    int res;
    char word[20];

    gettimeofday(&start, NULL);    /* start timer */
    do{
        res=fscanf(wordlist,"%s\n", word);
        CheckWord(W, word);
    }while(res!=EOF);

    gettimeofday(&end, NULL);    /* stop timer */
    elapsedTime = (end.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (end.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetCheckTime(W, elapsedTime);

    /* Reads words from wordlist(2)-RandJ
       calls CheckWord
       saves total Check time */
}

void Results(typosWords W, int totalwordsin)
{
    struct timeval start, end;
    double elapsedTime;
    char ap;
    int fileout;
    char arxeio[25];
    FILE* out;

    do{
        printf("Pathste O gia emfanisi apotelesmatwn sthn othoni\n");
        printf("        F gia emfanisi apotelesmatwn se arxeio keimenou\n");
        fflush(stdin);
        ap=getchar();

        if(ap=='O')
            fileout=0;
        else if(ap=='F'){
            fileout=1;
            printf("Dwse onoma arxeiou gia emfanisi apotelesmatwn (ston idio ypofakelo!)\n");
            scanf(" %s ", arxeio);
            if((out=fopen(arxeio, "w"))==NULL)
                printf("Sfalma kata to anoigma tou arxeiou\n");
        }
        else
            printf("Lathos apantisi\n");
    }while((ap!='O')&&(ap!='F'));

    gettimeofday(&start, NULL);    /* start timer */
    ShowCommonWords(out, W, fileout);
    gettimeofday(&end, NULL);    /* stop timer */

    elapsedTime = (end.tv_sec - start.tv_sec) * 1000.0;      /* sec to ms */
    elapsedTime += (end.tv_usec - start.tv_usec) / 1000.0;   /* us to ms */
    SetDiadromhTime(W, elapsedTime);
    fclose(out);

    PrintTimes(out, W, fileout, totalwordsin);
}





/* file: Words.c */



#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "typos_stoixeiouDDA.h"
#include "Words.h"

#if ylop == 'B'
    #include "BST_pointer-Recursive.h"
#elif ylop == 'A'
    #include "AVL_pointer.h"
#endif


extern char ylop;

#if ylop == 'B'             /* Ylopoihsh tou ATD-Words me aplo DDA(BST) */
typedef struct RecWords 
{
     typos_deikti WordsRiza;  /* to Words apoteleitai apo to DDA */
     float InsertTime [10];   /* xronoi eisagvghs ana 1024, 2048,... (7 sto synolo)
                                kai o xronos eisagogis OLWN twn leksevn
                                apo to epilegmeno arxeio eisodoy */
     float CheckTime;         /* xronos anazhthshs OLWN twn leksevn
                                apo to epilegmeno arxeio eisodoy */
     float DiadromhTime;      /* xronos diadromhs */
} RecWords;

typosWords dhmiourgia_Words()
{
   typosWords WordsNew=malloc(sizeof(RecWords));
    Tree_dimiourgia(&(WordsNew->WordsRiza));
   return WordsNew;         
}

void katastrofh_Words(typosWords * Wordsptr)
{
   Tree_katastrofi(&(*Wordsptr)->WordsRiza);
   free(*Wordsptr);
   *Wordsptr=NULL;
}

void InsertWord(typosWords Words, char * w)
{
    TStoixeioyTree stoixeio;
    int* error=0;

    strcpy(stoixeio.word, w);
    stoixeio.frequency=0;

    Tree_eisagogi(Words->WordsRiza, stoixeio, error);

    if(*error) printf("Sfalma eisagogis stoixeioy sto dentro\n");
     /*Input w
        sets stoixeio DDA (kai ta 2 melh)
        calls eisagogi_komvou sto DDA 
    */
}

void CheckWord(typosWords Words, char * w)
{
    TStoixeioyTree stoixeio;
    int* error=0;
    int* found=0;
    typos_deikti* deiktis;

    strcpy(stoixeio.word, w);
    stoixeio.frequency=0;

    Tree_anazitisi(Words->WordsRiza, stoixeio, deiktis, found);
    if(*found) deiktis->frequency++;

    /* Input w
        sets stoixeio DDA (kai ta 2 melh)
        calls anazitisi_komvou and
        ??if found
            calls periexomena and set???
    */
}

void ShowCommonWords(FILE *out, typosWords Words, int fileout)
{
    Tree_endodiat(Words->WordsRiza, fileout, out);
    /* diadromh DDA me parametro function for testing if In2nd==1 */
}

/* praxeis poy diaxeirizontai toys xronoys */

void SetInsertTime(typosWords Words, float time, int position)
{
    Words.InsertTime[position]=time;
    /* Input time, position
        sets Words.InsertTime[position]=time;
    */
}

void SetDiadromhTime(typosWords Words, float time)
{   
    Words.DiadromhTime=time;
    /* Input time
        sets Words.DiadromhTime=time;
    */
}

void PrintTimes(FILE *out, typosWords Words, int fileout, int totalwordsin)
{
    int i;

    if(!fileout){
        for(i=0; i<=6 ; i++){
            printf("Xronos eisagogis %d leksewn sto Words:        %f\n", 1024*(2^i), Words->InsertTime[i]);
        }
        printf("Xronos eisagogis OLWN twn leksewn sto Words:        %f\n", Words->InsertTime[7]);
        printf("Xronos plhrous endodiatetagmenis diadromhs:         %f\n", Words->DiadromiTime);
        printf("Mesos xronos diadromis ana leksi:                   %f\n", (Words->DiadromiTime)/totalwordsin);
    }
    else{
        for(i=0; i<=6 ; i++){
            fprintf(out, "Xronos eisagogis %d leksewn sto Words:        %f\n", 1024*(2^i), Words->InsertTime[i]);
        }
        fprintf(out, "Xronos eisagogis OLWN twn leksewn sto Words:        %f\n", Words->InsertTime[7]);
        fprintf(out, "Xronos plhrous endodiatetagmenis diadromhs:         %f\n", Words->DiadromiTime);
        fprintf(out, "Mesos xronos diadromis ana leksi:                   %f\n", (Words->DiadromiTime)/totalwordsin);
    }
    /* emfanizei sthn othoni InsertTimes, DiadromhTime */
}

#elif ylop == 'A'   /* Ylopoihsh tou ATD-Words me AVL-Tree */

typedef struct RecWords
{
     typos_deikti WordsRiza;  /* to Words apoteleitai apo to AVL */
     float InsertTime [10];   /* xronoi eisagvghs ana 1024, 2048,... (7 sto synolo)
                                kai o xronos eisagogis OLWN twn leksevn
                                apo to epilegmeno arxeio eisodoy */
     float CheckTime;         /* xronos anazhthshs OLWN twn leksevn
                                apo to epilegmeno arxeio eisodoy */
     float DiadromhTime;      /* xronos diadromhs */
} RecWords;

typosWords dhmiourgia_Words()
{
   typosWords WordsNew=malloc(sizeof(RecWords));
    AVLTree_dimiourgia(&(WordsNew->WordsRiza));
   return WordsNew;
}

void katastrofh_Words(typosWords * Wordsptr)
{
   AVLTree_katastrofi(&(*Wordsptr)->WordsRiza);
   free(*Wordsptr);
   *Wordsptr=NULL;
}

void InsertWord(typosWords Words, char * w)
{
    TStoixeioyTree stoixeio;
    int* error=0;

    strcpy(stoixeio.word, w);
    stoixeio.frequency=0;

    AVLTree_eisagogi(Words->WordsRiza, stoixeio, error);

    if(*error) printf("Sfalma eisagogis stoixeioy sto dentro\n");
     /*Input w
        sets stoixeio AVL (kai ta 2 melh)
        calls eisagogi_komvou sto AVL
    */
}

void CheckWord(typosWords Words, char * w)
{
    TStoixeioyTree stoixeio;
    int* error=0;
    int* found=0;
    typos_deikti* deiktis;

    strcpy(stoixeio.word, w);
    stoixeio.frequency=0;

    AVLTree_anazitisi(Words->WordsRiza, stoixeio, deiktis, found);
    if(*found) deiktis->frequency++;

    /* Input w
        sets stoixeio AVL (kai ta 2 melh)
        calls anazitisi_komvou and
        ??if found
            calls periexomena and set???
    */
}

void ShowCommonWords(FILE *out, typosWords Words, int fileout)
{
    AVLTree_endodiat(Words->WordsRiza, fileout, out);
    /* diadromh DDA me parametro function for testing if In2nd==1 */
}

/* praxeis poy diaxeirizontai toys xronoys */

void SetInsertTime(typosWords Words, float time, int position)
{
    Words.InsertTime[position]=time;
    /* Input time, position
        sets Words.InsertTime[position]=time;
    */
}

void SetDiadromhTime(typosWords Words, float time)
{
    Words.DiadromhTime=time;
    /* Input time
        sets Words.DiadromhTime=time;
    */
}

void PrintTimes(FILE *out, typosWords Words, int fileout, int totalwordsin)
{
    int i;

    if(!fileout){
        for(i=0; i<=6 ; i++){
            printf("Xronos eisagogis %d leksewn sto Words:        %f\n", 1024*(2^i), Words->InsertTime[i]);
        }
        printf("Xronos eisagogis OLWN twn leksewn sto Words:        %f\n", Words->InsertTime[7]);
        printf("Xronos plhrous endodiatetagmenis diadromhs:         %f\n", Words->DiadromiTime);
        printf("Mesos xronos diadromis ana leksi:                   %f\n", (Words->DiadromiTime)/totalwordsin);
    }
    else{
        for(i=0; i<=6 ; i++){
            fprintf(out, "Xronos eisagogis %d leksewn sto Words:        %f\n", 1024*(2^i), Words->InsertTime[i]);
        }
        fprintf(out, "Xronos eisagogis OLWN twn leksewn sto Words:        %f\n", Words->InsertTime[7]);
        fprintf(out, "Xronos plhrous endodiatetagmenis diadromhs:         %f\n", Words->DiadromiTime);
        fprintf(out, "Mesos xronos diadromis ana leksi:                   %f\n", (Words->DiadromiTime)/totalwordsin);
    }
    /* emfanizei sthn othoni InsertTimes, DiadromhTime */
}


#endif







/* file: Words.h */


#ifndef __TYPOS_WORDS__ 
#define __TYPOS_WORDS__
#include <stdio.h>

/*orismos typou Words */
typedef struct RecWords * typosWords; 

/* epikefalides praxewn */

/* praxeis poy yloioyntai me praxeis DDA */
typosWords dhmiourgia_Words();
void katastrofh_Words(typosWords * Wordsptr);

void InsertWord(typosWords Words, char * w);
void CheckWord(typosWords Words, char * w);
void ShowCommonWords(FILE *out, typosWords Words, int fileout);

/* praxeis poy diaxeirizontai toys xronoys */
void SetInsertTime(typosWords Words, float time, int position);
void SetCheckTime(typosWords Words, float time);
void SetDiadromhTime(typosWords Words, float time);
void PrintTimes(FILE *out, typosWords Words, int fileout, int totalwordsin);
#endif

命令行是: gcc -c Words.c main.c 和其他三个没有问题的源代码文件

(没问题)

然后 gcc -o Words.o main.o 和上面编译的三个目标文件

(这里弹出“ undefined reference ”)

与单个相同:gcc -o test Words.c main.c 和其他三个源代码文件

(再次弹出“ undefined reference ”)

最佳答案

在某些地方,您将 ylop 视为变量名,而在其他地方,您将其视为预处理器符号,您似乎没有在任何地方定义它。您需要先解决这个问题,然后才能正常工作。

关于c - 链接器错误 undefined reference ... c 中包含的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16693630/

相关文章:

c++ - 在静态库中发现对成员函数的 undefined reference

c - GCC:对 xxx 的 undefined reference

c - 我如何定期从 c 中允许值的位掩码中选择值?

c - 如何读取acct文件?

c - SPARC 和 x86 GCC 一个 C 代码的不同结果

C++如何为 map 制作浅拷贝构造函数

c++ - 无法调用函数引用 c++

c - 如何知道应用程序的启动点

c - linkedList - 段错误(核心转储)

Javascript - 有时只能修改传递给函数的数组