2010-11-24 74 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()它需要任何啓動程序來執行程序。基本上主要是一個程序執行時由編譯器識別的標識符。