2016-08-20 19 views
-3

我在Eclipse中創建了一個C項目,之後嘗試添加源文件並編寫小程序。它第一次運行平穩。但是當我添加另一個源文件並在main()中寫入代碼並構建它時,它會引發一些錯誤。如何在Eclipse中編寫不同的C程序,所有這些程序都有自己的main()?

+1

這將真正幫助,如果你說這是什麼*一些錯誤*消息說...以及你的兩個源文件的代碼是什麼樣的。 – Filburt

+0

我懷疑你有'main'方法的多個文件。只有一個'main'可以存在,您最好用一個'main'方法創建一個'index'文件,您可以使用'switch'或其他機制來調用其他文件中的方法,這取決於哪個小你想運行的程序(基於可能的命令行參數)。 –

+0

我的項目名稱是C. 它裏面我有兩個源文件: 1. hi.c和 2.你好World.c 這是罰款,直至或除非有HelloWorld的文件。 在編寫hello world的兩行代碼後,只打印「Hello World聲明」,我構建了該項目。 然後它拋出錯誤,自動在hi.c的main()旁邊做了一個紅色十字。並且聲明是:: 「」「此行的多個標記 \t - 多個」主「的定義 \t - 行斷點:嗨。c [line:2]「」「 – kulst

回答

0

由於您試圖在一個程序中放置兩個main()(即兩個起點),Eclipse無法構建您的代碼。如果一個程序有多個起點,計算機如何知道從哪裏開始?

您應該創建另一個項目來編寫另一個程序。

+0

是不是可以在一個項目中編寫不同的程序(例如:迴文,矩陣加法等) 我的意思是說,在Java中我們可以創建一個包,我們可以有不同的java類,它們都具有不同的main() – kulst

+0

@ user6737558可能,但即使有可能,也不應該這樣做,因爲如果每個項目包含哪個項目多個程序 – dorukayhan

+0

Java項目有點不同當你嘗試用多個'public static void main'構建一個Java項目時,Eclipse會詢問你選擇哪個主要起點,然後做一些黑魔法來防止其他主線成爲起點 – dorukayhan

1

比方說你有兩個方案:

計劃1個

#include <stdlib.h> 
#include <stdio.h> 


int main(int argc, char ** argv) 
{ 
    printf("I am program 1.\n"); 

    return EXIT_SUCCESS; 
} 

計劃2

#include <stdlib.h> 
#include <stdio.h> 


int main(int argc, char ** argv) 
{ 
    printf("I am program 2.\n"); 

    return EXIT_SUCCESS; 
} 

現在,你想將它們合併到一個程序。

重命名並添加新的main()

#include <stdlib.h> 
#include <stdio.h> 


int p1(int argc, char ** argv) 
{ 
    printf("I am program 1.\n"); 

    return EXIT_SUCCESS; 
} 

int p2(int argc, char ** argv) 
{ 
    printf("I am program 2.\n"); 

    return EXIT_SUCCESS; 
} 

int main(int argc, char ** argv) 
{ 
    printf("I am providing program 1 and program 2.\n"); 

    printf("Enter 1 for 'program 1' or 2 for 'program 2' (any other key exits).\n"); 

    { 
    int result = EXIT_FAILURE; 

    int c = getchar(); 
    switch (c) 
    { 
     case '1': 
     result = p1(argc, argv); 
     break;   

     case '2': 
     result = p2(argc, argv); 
     break; 

     default: 
     break; 
    } 

    return result; 
    } 
} 

要具有這種分裂了各種文件以模塊化的方式,你可以這樣做:

p1.h

#ifndef P1_H 
#define P1_H 

int p1(int argc, char ** argv); 


#endif 

p1.c

#include <stdlib.h> 
#include <stdio.h> 

#include "p1.h" 


int p1(int argc, char ** argv) 
{ 
    printf("I am program 1.\n"); 

    return EXIT_SUCCESS; 
} 

p2.h

#ifndef P2_H 
#define P2_H 

int p2(int argc, char ** argv); 


#endif 

p2.c

#include <stdlib.h> 
#include <stdio.h> 

#include "p2.h" 


int p2(int argc, char ** argv) 
{ 
    printf("I am program 1.\n"); 

    return EXIT_SUCCESS; 
} 

的main.c

#include <stdlib.h> 
#include <stdio.h> 

#include "p1.h" 
#include "p2.h" 


int main(int argc, char ** argv) 
{ 
    printf("I am providing program 1 and program 2.\n"); 

    printf("Enter 1 for 'program 1' or 2 for 'program 2' (any other key exits).\n"); 

    { 
    int result = EXIT_FAILURE; 

    int c = getchar(); 
    switch (c) 
    { 
     case '1': 
     result = p1(argc, argv); 
     break;   

     case '2': 
     result = p2(argc, argv); 
     break; 

     default: 
     break; 
    } 

    return result; 
    } 
} 
+0

感謝@alk所有的討論,現在對我來說都是清楚的..我已經嘗試過使用上述公司自己的ncept。 您能否描述頭文件的功能? – kulst

+0

@ user6737558:由於C不支持使用標題(.h)進行名稱修飾(名稱修飾),如圖所示,它將它們包括在每個相關翻譯單元(.c)中,允許編譯器檢測函數的實現和他們被稱爲匹配的方式。在抽象級別上(按照慣例,而不是在語言級別上),頭文件可以作爲翻譯單元接口聲明。 – alk

相關問題