簡體   English   中英

鏈接器錯誤未定義對…C中包含的庫的引用

[英]Linker error undefined reference to… an included library in c

我為此進行了很多搜索,但我真的無法弄清楚。 我的項目包含5個.c文件和4個.h標頭。 全部在同一個文件夾中。 我成功地分別編譯了它們,但是當涉及到組編譯時(gcc -o progname和linux環境中的所有代碼文件或dev-c ++中的compile-all選項),在兩種情況下我都遇到這些錯誤:

 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中實現

我首先以為我錯過了標題#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和上面編譯中的三個目標文件

(在這里彈出“未定義的引用”)

與單一代碼相同:gcc -o測試Words.c main.c和其他三個源代碼文件

(再次彈出“未定義的引用”)

在某些地方,您將ylop視為變量名,而在其他地方,您將其視為預處理器符號,您似乎沒有在任何地方定義它。 您需要先進行修復,然后再進行任何操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM