c++ - 错误 LNK2005 : _main already defined in hold. 对象

标签 c++ c visual-c++

您好,我浏览了所有相同的错误,但我没有解决我的问题,所以我使用的是 MS VC++ 2010,我有两个文件a.c 和 b.c, 每个单独工作都没有错误,每个都有一个简单明了的代码。但是当我使用它们收集时显示此错误 **error LNK2005: _main already defined in a.c ** 在代码块 IED 上显示同样的错误。我认为这是指两次使用主要功能。现在我如何为两个文件使用一个主要功能

代码文件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();
}

代码文件b.c

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

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();
}

代码文件b.c

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

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

void b_main()
{
    int x=10;

    printf("%d", x);
    getch();
}

请注意,仔细命名函数是一种很好的做法,以便名称描述它们的作用。例如,在此示例中,您可以调用 a_main PrintIs7OrNot 和 b_main Print10。

关于c++ - 错误 LNK2005 : _main already defined in hold. 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26583763/

相关文章:

c++ - Visual C++ 和 C++ 生成器

python - 使用命令行参数在 C++ 中运行 python .py 文件

c - 为什么输出给出这样的值?

c - 无复制类型注解

c++ - Visual C++::'malloc' 中的奇怪错误:函数不带 1 个参数

c++ - 如何在 Visual C++ 中将数组元素向右移动并用字符串替换移动后的索引

C++如何使函数指针指向类方法

c++ - multimap 像拼接 - 添加第三个图像

c++ - 在 C++ 中声明变量与动态分配内存给变量?

c - 总线错误排查