我在Eclipse中創建了一個C項目,之後嘗試添加源文件並編寫小程序。它第一次運行平穩。但是當我添加另一個源文件並在main()中寫入代碼並構建它時,它會引發一些錯誤。如何在Eclipse中編寫不同的C程序,所有這些程序都有自己的main()?
回答
由於您試圖在一個程序中放置兩個main()
(即兩個起點),Eclipse無法構建您的代碼。如果一個程序有多個起點,計算機如何知道從哪裏開始?
您應該創建另一個項目來編寫另一個程序。
是不是可以在一個項目中編寫不同的程序(例如:迴文,矩陣加法等) 我的意思是說,在Java中我們可以創建一個包,我們可以有不同的java類,它們都具有不同的main() – kulst
@ user6737558可能,但即使有可能,也不應該這樣做,因爲如果每個項目包含哪個項目多個程序 – dorukayhan
Java項目有點不同當你嘗試用多個'public static void main'構建一個Java項目時,Eclipse會詢問你選擇哪個主要起點,然後做一些黑魔法來防止其他主線成爲起點 – dorukayhan
比方說你有兩個方案:
計劃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;
}
}
- 1. 如何編寫沒有'main'的C程序?
- 2. 如何在自己的java編寫程序中控制另一個程序?
- 3. 在C#編寫自己的安裝程序的「創建者」
- 4. 編譯並運行沒有main()的程序在C中爲
- 5. 程序沒有main()的
- 6. 程序的每一行都有自己的地址嗎?
- 7. 我如何讓每個listitem中的圖像都有自己的處理程序?
- 8. 如何在Eclipse中創建我自己的實用程序類?
- 9. 如何編寫我自己的Hadoop調度程序?
- 10. 如何編寫自己的基本磁盤掃描程序
- 11. 如何使用.NET編寫我自己的Global Snippets程序?
- 12. 編寫自定義Eclipse驗證程序
- 13. 爲什麼這些程序(僅在一個printf語句中有所不同)具有不同的輸出?
- 14. 如何在C程序(AVR)中編寫程序集ISR?
- 15. 如何編寫程序以獲取表格的所有數據?
- 16. 爲什麼這個C程序中所有的輸出都是一樣的?
- 17. C程序沒有完成所有的步驟在這裏
- 18. XML程序沒有編寫XML C#
- 19. svg linejoin imlementation在不同的應用程序中有所不同
- 20. C編程語言,編寫有用的程序
- 21. C:每次運行程序都會有不同的值
- 22. 每個操作系統是否都有自己的彙編程序?
- 23. 用c編程在我自己的exe中編寫/追加
- 24. 如何在Prolog中編寫/編輯自己的協程?
- 25. 編寫C++程序的Makefile
- 26. C程序都沒有在我的linux卡利編譯
- 27. 爲什麼這些小型D程序的行爲有所不同?
- 28. 如何在java中編寫我自己的桌面共享應用程序?
- 29. 如何訪問所有JFrames在同一應用程序的不同線程
- 30. 包括現有的應用程序如在自己的應用程序片段
這將真正幫助,如果你說這是什麼*一些錯誤*消息說...以及你的兩個源文件的代碼是什麼樣的。 – Filburt
我懷疑你有'main'方法的多個文件。只有一個'main'可以存在,您最好用一個'main'方法創建一個'index'文件,您可以使用'switch'或其他機制來調用其他文件中的方法,這取決於哪個小你想運行的程序(基於可能的命令行參數)。 –
我的項目名稱是C. 它裏面我有兩個源文件: 1. hi.c和 2.你好World.c 這是罰款,直至或除非有HelloWorld的文件。 在編寫hello world的兩行代碼後,只打印「Hello World聲明」,我構建了該項目。 然後它拋出錯誤,自動在hi.c的main()旁邊做了一個紅色十字。並且聲明是:: 「」「此行的多個標記 \t - 多個」主「的定義 \t - 行斷點:嗨。c [line:2]「」「 – kulst