错误 LNK2005:_main 已在 Hold.obj 中定义 [英] error LNK2005: _main already defined in hold.obj

查看:31
本文介绍了错误 LNK2005:_main 已在 Hold.obj 中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

推荐答案

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

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

代码文件 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.

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.

这篇关于错误 LNK2005:_main 已在 Hold.obj 中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆