Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误LNK2005:_main已在hold.obj中定义_C++_C_Visual C++ - Fatal编程技术网

C++ 错误LNK2005:_main已在hold.obj中定义

C++ 错误LNK2005:_main已在hold.obj中定义,c++,c,visual-c++,C++,C,Visual C++,您好,我浏览了所有相同的错误,但我没有得到解决我的问题,所以我使用MS VC++2010,我有两个文件a.c和b.c,每个文件单独工作没有错误,每个文件都有一个简单的代码和清晰的代码。但是,当我使用它们进行收集时,会显示此错误**错误LNK2005:_main已在a.c**中定义,此错误会显示在代码块IED上。我认为这指的是两次使用main函数。现在我如何为这两个文件使用一个主函数 代码文件a.c #include<stdio.h> #include<conio.h>

您好,我浏览了所有相同的错误,但我没有得到解决我的问题,所以
我使用MS VC++2010
,我有两个文件
a.c和b.c,
每个文件单独工作没有错误,每个文件都有一个简单的代码和清晰的代码。但是,当我使用它们进行收集时,会显示此错误
**错误LNK2005:_main已在a.c**
中定义,此错误会显示在代码块IED上。我认为这指的是两次使用main函数。现在我如何为这两个文件使用一个主函数

代码文件a.c

#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();
}
#包括
#包括
main()
{
INTA=9;
如果(a==7)
{
看跌期权(“这是第七号”);
}
其他的
{
看跌期权(“这不是第七号”);
}
getch();
}
代码文件b.c

#include<stdio.h>
#include<conio.h>

main()
{
    int x=10;

    printf("%d", x);
    getch();
}    
#include <stdio.h>
#include <conio.h>

void main()
{
   a_main();
   b_main();
}

void b_main()
{
    int x=10;

    printf("%d", x);
    getch();
}
#包括
#包括
main()
{
int x=10;
printf(“%d”,x);
getch();
}    

不可能有两个主功能,一个程序只能在一个主功能中运行。您可以重命名主函数,并创建一个同时调用它们的主函数

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();
}
代码文件a.c
#包括
#包括
作废a_main()
{
INTA=9;
如果(a==7)
{
看跌期权(“这是第七号”);
}
其他的
{
看跌期权(“这不是第七号”);
}
getch();
}
代码文件b.c

#include<stdio.h>
#include<conio.h>

main()
{
    int x=10;

    printf("%d", x);
    getch();
}    
#include <stdio.h>
#include <conio.h>

void main()
{
   a_main();
   b_main();
}

void b_main()
{
    int x=10;

    printf("%d", x);
    getch();
}
#包括
#包括
void main()
{
a_main();
b_main();
}
void b_main()
{
int x=10;
printf(“%d”,x);
getch();
}

注意,对函数进行精确命名是一种很好的做法,以便名称描述它们的功能。例如,在本例中,您可以将a_main print称为7ornot和b_main print称为10。

您可以发布a.c和b.c的代码吗?这真的没有任何意义。另外,如果使用两个主函数有问题,请创建
exec_main.c
文件,并将主函数放在其中。
现在如何为两个文件使用一个主函数
定义一个
main
函数。@user657267正确!此外,您知道所有内容都从一个主线程运行,因此无论如何都不能有两个。链接器告诉您,您定义了两次
main
,因为您定义了两次
main
。但是,请问我如何解决此问题?@Basil-有多少?您是否需要在一个程序中包含所有这些主要功能,或者它们可以分别位于各自的程序中?你可以继续:c_main、d_main、e_main等等。真的谢谢Scott,我想问你另一个问题,现在主要的活动文件是a.c正在运行它的代码,那么我怎样才能使b.c成为活动文件来运行它的代码?@Basil-对不起,我不明白你的问题。如果您使用我在答案中给出的示例,那么a_main()应该运行,然后b_main()应该运行。如果您愿意,您可以更改main()中的代码,将这两个函数按其他顺序排列。@ScottLangham我想问一个关于同一主题的问题,如果我想分别运行这两个程序,该怎么办?我是否必须为一个新程序创建另一个新项目,或者我可以通过添加新文件在同一个项目中创建新项目?@AsheeshSahu这取决于您编写程序时使用的工具。C++没有一个叫做Project的概念,但是你用来编写程序的工具可以将文件组织成项目。我使用VisualStudio编写C++,在VisualStudio中,每个程序都需要一个新项目。