C++ 错误 LNK2005:_main 已在 Hold.obj 中定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26583763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error LNK2005: _main already defined in hold.obj
提问by Basil
Hi Please i have browsed all same error that i got but I didnt get solving for my problem, so I am using MS VC++ 2010
and i have two files a.c and b.c,
each one works no error alone and each one has a simple code and clear. But when i use them to gather shows this error **error LNK2005: _main already defined in a.c **
this same error shows on Code block IED. I think that refer to using main function twice. Now how can i use one main function for both file
嗨,请我浏览了所有相同的错误,但我没有解决我的问题,所以I am using MS VC++ 2010
我有两个文件,a.c and b.c,
每个文件都没有单独的错误,每个文件都有一个简单的代码和清晰的代码。但是当我使用它们收集显示此错误时**error LNK2005: _main already defined in a.c **
,代码块 IED 上也会显示相同的错误。我认为是指两次使用 main 函数。现在我如何为两个文件使用一个主要功能
Code file a.c
代码文件ac
#include<stdio.h>
#include<conio.h>
main()
{
int a =9;
if(a==7)
{
puts("This is number seven ");
}
else
{
puts("This isn't number seven ");
}
getch();
}
Code file b.c
代码文件 bc
#include<stdio.h>
#include<conio.h>
main()
{
int x=10;
printf("%d", x);
getch();
}
回答by Scott Langham
It is not possible to have two main functions, a program starts running in only 1 main function. You could rename the main functions, and create one main function that calls them both.
不可能有两个主函数,一个程序只在一个主函数中开始运行。您可以重命名主函数,并创建一个同时调用它们的主函数。
Code file a.c
#include <stdio.h>
#include <conio.h>
void a_main()
{
int a =9;
if(a==7)
{
puts("This is number seven ");
}
else
{
puts("This isn't number seven ");
}
getch();
}
Code file b.c
代码文件 bc
#include <stdio.h>
#include <conio.h>
void main()
{
a_main();
b_main();
}
void b_main()
{
int x=10;
printf("%d", x);
getch();
}
Note, it is good practice to carfully name functions so that the names describe what they do. Eg, in this example you might call a_main PrintIs7OrNot and b_main Print10.
请注意,谨慎地命名函数是一种很好的做法,以便名称描述它们的作用。例如,在本例中,您可以调用 a_main PrintIs7OrNot 和 b_main Print10。