对'main'的未定义引用 - collect2:error:ld返回1退出状态

时间:2017-04-23 23:18:48

标签: c reference undefined collect

我正在尝试在UNIX环境中编译并继续收到此错误。但是,我所拥有的只是文件中的主要功能?有任何想法吗?这是我唯一的代码,因为我在另一个文件中遇到错误,并决定使用JUST主要功能测试编译,如果包含头文件。我删除了头文件的include语句,它编译得很好。我已经尝试过gcc filename headfilename,只是为了看看它是否有所作为,但是,它没有。头文件位于同一文件夹中。

有什么想法吗?

以下是代码:

#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

1 个答案:

答案 0 :(得分:1)

您的标题功能将main定义为demoted_main这意味着您的程序没有主要功能且无法与gcc链接。为了正确链接您的程序,您必须删除该行。您还可以使用链接器选项将demoted_main用作入口点。这可以通过gcc -o TriePrediction.c TriePrediction.h -Wl,-edemoted_main -nostartfiles进行,但不建议使用。

相关问题