2017-04-23 20 views
-1

私はUNIX環境でコンパイルしようとしていますが、このエラーを受け続けます。しかし、私が持っているのはファイルの主な機能です。何か案は?これは、私が別のファイルでエラーが発生して以来、私が持っている唯一のコードであり、ヘッダーファイルが含まれていればmain関数でコンパイルをテストすることに決めました。私は、ヘッダーファイルのインクルードステートメントを削除し、ちょうど良いコンパイルします。私はgccのファイル名headfilenameを試してみましたが、違いがあるかどうかを確認するだけですが、そうではありません。ヘッダーファイルは同じフォルダーにあります。`main 'への未定義の参照 - collect2:エラー:ldが1の終了ステータスを返しました

アイデア?

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

次の行を指定してコンパイル:gccのTriePrediction.c

私も試してみました

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


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

これは私が取得しています正確なエラーがある:ここでは

はコードです:

gcc TriePrediction.c TriePrediction.h 

注:は、main関数が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

あなたのヘッダー機能は、これはあなたのプログラムが主な機能を持っていないとgccとリンクすることができないことを意味demoted_mainするmainを定義しています。プログラムを正しくリンクさせるには、その行を削除する必要があります。リンカオプションを使用して、demoted_mainをエントリポイントとして使用することもできます。これはgcc -o TriePrediction.c TriePrediction.h -Wl,-edemoted_main -nostartfilesで可能ですが、お勧めしません。

+0

ありがとう、これは多くの意味があります! – starlight

関連する問題