2010-11-24 79 views
3

我做了一个仍在开发中的程序。我没有故意在程序中声明main。当我开发一个图库和其他算法时,我将在我的程序中使用它。在C语言中开发这样一个库的目的是为了解决在介绍算法Thomas H Coreman 这里是代码,如果有人想看。为什么在程序中主要需要

#include<stdio.h> 
#include<stdlib.h> 
#define GREY 1 
#define BLACK 0 
#define WHITE 2 
typedef struct node *graph; 

graph cnode(int data);  //cnode is to create a node for graph 
void cgraph(void); 
struct node { 
    int data, color; 
    struct node *LEFT, *RIGHT, *TOP, *DOWN; 
}; 

graph root = NULL; 

void cgraph(void) 
{ 
    int n, choice, dir, count; 

    choice = 1; 
    count = 1; 
    graph priv, temp; 

    printf("Printf we are making a graph the first is root node\n"); 
    while (choice == 1) { 
     count++; 
     if (count == 1) { 
      printf("This is going to be root node \n"); 
      scanf("%d", &n); 
      root = cnode(n); 
      count--; 
      priv = root; 
     }  //ending if 
     else { 
      printf 
       ("Enter direction you want to go LEFT 1 RIGHT 2 TOP 3 DOWN 4\n"); 
      scanf("%d", &dir); 
      printf("Enter the data for graph node\n"); 
      scanf("%d", &n); 
      temp = cnode(n); 
      if (dir == 1) { 
       priv->LEFT = temp; 
      } 
      if (dir == 2) { 
       priv->RIGHT = temp; 
      } 
      if (dir == 3) { 
       priv->TOP = temp; 
      } 
      if (dir == 4) { 
       priv->DOWN = temp; 
      } 
      priv = temp; 
     }  //ending else 
     printf 
      ("Enter 1 to continue adding nodes to graph any thing else would take you out\n"); 
     scanf("%d", &choice); 
    }   //ending while 
}    //ending main 

graph cnode(int data) 
{ 
    graph temp = (graph) malloc(sizeof(graph)); 

    temp->data = data; 
    temp->LEFT = NULL; 
    temp->RIGHT = NULL; 
    temp->TOP = NULL; 
    temp->DOWN = NULL; 
    temp->color = -1; 
    return temp; 
} 

当我编译上述程序时,我得到以下错误。

cc graph.c 
/usr/lib/gcc/x86_64-linux-gnu/4.4.3/../../../../lib/crt1.o: In function `_start': 
(.text+0x20): undefined reference to `main' 
collect2: ld returned 1 exit status 

这个错误是什么意思,为什么我应该在我的程序中声明main?

回答

4

为什么?因为该标准如此说(主要)。

main函数是托管C环境所必需的(允许独立环境以任何他们喜欢的方式启动)。

如果您正在开发库,库本身不需要main,但如果没有库(不使用非便携式技巧除外),将无法将其变为可执行文件。而且,至少,你应该有一个测试套件。

换句话说,你的程序库应该有一个大型的测试套件,它可以通过一个main函数来控制(很可能在一个单独的源文件中),这样你就可以测试任何新的工作和回归测试以确保它没有没有完成旧工作。

5

main如果您将代码构建到应用程序中,则必须存在,因为主函数可用作应用程序的入口点。

但是,如果您的代码正在构建为lib,那么不需要main。

编辑:检查this有关静态和共享库的信息。

+0

雅我想为我的程序做一个库,所以我会继续编译检查代码,所以我该怎么做才能摆脱这些错误? – 2010-11-24 07:46:36

+0

@Bond,我编辑了我的答案,并提供了一个链接,介绍如何构建静态库和共享库。无论如何检查代码,你只能编译它,并避免链接它。使用gcc -c graph.c作为caf建议仅用于编译。 – Jay 2010-11-24 12:48:07

14

默认情况下,gcc(和大多数C编译器)编译并链接到独立的可执行文件。 main()函数是必需的,以便启动代码知道代码的执行应该在哪里启动。

要编译不带链接的库代码,请使用gcc -c graph.c。在这种情况下,graph.c不需要main()函数。

0

主要是需要执行您的程序。当您尝试执行用C编写的程序时,它将转到主函数并从此处执行。 如果你正在写一个库,你最好写一个简单的测试代码来调用你的库函数,编译并运行该测试程序来测试你的库。

1

程序需要一个入口点来阐明程序的启动位置。没有这些,你的工具就不可能知道应该首先调用哪个函数。

可以指定另一个函数作为入口点,但通过使用main,读取代码的每个人都会知道程序的启动位置。

通常,在开发库时,你将把main放在一个单独的程序中,并在测试你的库时用它作为起点。类似这样的:

gcc -o library.o -c library.c 
gcc -o main.o -c main.c 
gcc -o testprogram library.o main.o 
0

从形式上讲,这是从加载程序而不是编译程序的要求。程序员会知道每个程序在执行前都应该加载到上下文中。这是装载者的责任,但是如果没有标准来定义'入口点',装载者不知道哪一行代码是入口点。

+0

此外,如果您只是构建目标文件或lib,那不会链接到可执行文件。这种情况下,“主”入口点不是必需的。 – 2010-11-24 08:48:15

1

通常main()自己启动。如果您忽略main()它需要任何启动程序来执行程序。基本上主要是一个程序执行时由编译器识别的标识符。