undefined reference to `main' - collect2: error: ld returned 1 exit status

undefined reference to `main' - collect2: error: ld returned 1 exit status

我正在尝试在 UNIX 环境中编译并不断收到此错误。但是,我只有文件中的主要功能?有任何想法吗?这是我拥有的唯一代码,因为我在另一个文件中遇到错误,并决定在包含头文件的情况下仅使用 main 函数测试编译。我删除了头文件的 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

您的头函数将 main 定义为 demoted_main,这意味着您的程序没有 main 函数并且不能用 gcc linked。为了使您的程序 link 正确,您必须删除该行。您还可以使用 linker 选项将 demoted_main 用作您的入口点。 gcc -o TriePrediction.c TriePrediction.h -Wl,-edemoted_main -nostartfiles 可以做到这一点,但不推荐。