2017-04-23 75 views
-1

我試圖在UNIX環境下編譯並不斷收到此錯誤。但是,我擁有的只是文件中的主要功能?有任何想法嗎?這是我的唯一代碼,因爲我在不同的文件中有錯誤,並決定在包含頭文件的情況下使用主函數測試編譯。我刪除了headerfile的include語句,它編譯得很好。我已經嘗試了gcc文件名的頭文件名,只是爲了看看它會有所作爲,但是,它不。頭文件位於相同的文件夾中。對`main'的未定義引用 - collect2:錯誤:ld返回1退出狀態

任何想法?

下面的代碼:

#include "TriePrediction.h" 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 


int main(int argc, char **argv) 
{ 
    return 0; 
} 

這是確切的錯誤我收到:

In function `_start': 
(.text+0x18): undefined reference to `main' 
collect2: error: ld returned 1 exit status 

與下面的行編譯:GCC TriePrediction.c

我也嘗試:

gcc TriePrediction.c TriePrediction.h 

主要功能位於TriePrediction.c

這是頭文件:

注:我刪除我建立的文件中編譯原因的功能的地方,所以我知道這是錯的,但是,我這樣做是爲了看看是否搞亂了未定義的引用錯誤的編譯。

#ifndef __TRIE_PREDICTION_H 
#define __TRIE_PREDICTION_H 

#define MAX_WORDS_PER_LINE 30 
#define MAX_CHARACTERS_PER_WORD 1023 

// This directive renames your main() function, which then gives my test cases 
// a choice: they can either call your main() function (using this new function 
// name), or they can call individual functions from your code and bypass your 
// main() function altogether. THIS IS FANCY. 
#define main demoted_main 

typedef struct TrieNode 
{ 
    // number of times this string occurs in the corpus 
    int count; 

    // 26 TrieNode pointers, one for each letter of the alphabet 
    struct TrieNode *children[26]; 

    // the co-occurrence subtrie for this string 
    struct TrieNode *subtrie; 
} TrieNode; 


// Functional Prototypes 

TrieNode *buildTrie(char *filename); 

TrieNode *destroyTrie(TrieNode *root); 

TrieNode *getNode(TrieNode *root, char *str); 

void getMostFrequentWord(TrieNode *root, char *str); 

int containsWord(TrieNode *root, char *str); 

int prefixCount(TrieNode *root, char *str); 

double difficultyRating(void); 

double hoursSpent(void); 

#endif 
+0

請顯示您的確切構建命令行。 – kaylum

+0

是否有任何宏在麻煩的頭文件中重新定義'main'? – InternetAussie

+0

@kaylum已更新 – starlight

回答

1

你的頭功能是定義maindemoted_main,這意味着你的程序沒有一個主要功能,不能用gcc鏈接。爲了讓您的程序正確鏈接,您必須刪除該行。您也可以使用鏈接器選項來使用demoted_main作爲您的入口點。這可能與gcc -o TriePrediction.c TriePrediction.h -Wl,-edemoted_main -nostartfiles但不推薦。

+0

謝謝,這樣做很有道理! – starlight

相關問題