主要优先级的多重定义在这里 [英] multiple definition of main first defined here

查看:112
本文介绍了主要优先级的多重定义在这里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的新手,目前正在学习C编程.我正在使用GCC编译器在代码块及其上编写代码.当我创建一个新项目时(如您所知,它会使用它创建main.c文件),因此,我无法在该项目中编译另一个文件.

I'm new to programming and currently I'm learning C programming. I'm writing codes on the code blocks and in it using GCC compiler. When I create a new project, (as you know it creates main.c file with it) and due to that I'm not able to compile another file in that project.

文件1:

 #include<stdio.h>

int main()
{
    int a,b,c,d;
    printf("Enter three numbers\n");
    scanf("%d%d%d",&a,&b,&c);
    d=a;
    if(b>d)
        d=b;
    if(c>d)
        d=c;
    printf("\n The maximum of three numbers is %d",d);

}

文件2:main.c

File 2: main.c

#include <stdio.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

当我编译第一个程序时,它显示以下错误: 主要"的多重定义 首先在这里定义

When I compile the first programme, it shows the following error: multiple definition of 'main' first defined here

我已经搜寻了所有可能的地方,但无法解决.在堆栈溢出的答案之一中,有人建议将其写在 (项目->生成选项...->链接器设置(选项卡))

I've searched every where I could and I'm not able to solve this. In one of the answers here on stack overflow, someone had suggested to write this in (Project->Build options...->Linker settings (tab))

-Wl,--allow-multiple-definition

当我写它的时候,没有错误.但是它无法运行我的文件1,而是运行了main.c文件.即使关闭了main.c文件,它也会再次打开并运行main.c文件,该文件的输出为"Hello World!".

When I wrote it, there were no errors. But it wasn't able to run my File 1 and instead, it runs that main.c file. Even when I close the main.c file, it opens there again and runs main.c file which gives the output "Hello World!".

最初,当我使用代码块时,没有这样的错误.我不知道为什么会这样,而且我对编译器的了解也不多.

Initially when I was using code blocks there were no such errors. I don't know why this is happening and I've not much knowledge about compilers.

推荐答案

如注释中所述,您只能具有一个主要功能.

As noted in comments you can only have one main function.

因此,当您开始一个新项目时,需要用要使用的main.c文件替换main.c文件.或者,您可以编辑"hello world" main.c程序.

So when you start a new project you need to replace the main.c file with the main.c file you want to use. Or you can edit the 'hello world' main.c program.

使用code :: blocks启动新项目时,可以使用打印"Hello World"的简单程序获取新目录.该文件通常是main.c.您需要编辑或替换此文件. code :: blocks将这个简单的main.c程序放在新项目中的原因是,您无需编写新程序就可以对其进行编译和测试系统.

When you start a new project in code::blocks you can get a new directory with a simple program that prints 'Hello World'. This file is usually main.c. You need to edit this file or replace it. The reason that code::blocks puts this simple main.c program in the new project is so that you can compile it and test your system without having to write a new program.

这篇关于主要优先级的多重定义在这里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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