首页 > 解决方案 > 如何解决“'main()'的多重定义”C编译错误?

问题描述

我最近开始学习 C 并且必须创建一个程序,从标准输入中扫描两个整数值,用空格分隔,然后 printf 这两个整数的总和。必须能够接受负值。我正在使用 repl.it 编写代码 1st 然后粘贴到 .c 中进行编译。

试图:

#include <stdio.h>

int main() {    

    int number1, number2, sum;

    printf("Enter two integers: ");

    scanf("%d", &number1);

    scanf("%d", &number2);

    // calculating sum

    sum = number1 + number2;      

    printf("%d + %d = %d", number1, number2, sum);

    return 0;
}

[OP最初说“除了这个打印”——但这不是程序输出——这是程序运行之前编译过程中的错误输出]

除非我尝试编译 IDE 输出错误

/tmp/t2-8eec00.o: In function `main':
t2.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/t3-72a7ab.o: In function `main':
t3.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
/tmp/main-2c962b.o: In function `main':
main.c:(.text+0x0): multiple definition of `main'
/tmp/t1-f81f83.o:t1.c:(.text+0x0): first defined here
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)
exit status 1

输出错误,我犯了什么错误?获得期望值的正确方法是什么?

(例如 1+2=3)

使用它的平台:

https://imgur.com/a/9E8RzAO

标签: c

解决方案


这是一个项目管理问题。IDE 显示您有 4 个文件,所有这些文件都相互冲突。你有t1.c, t2.c,t3.cmain.c. 他们都试图定义main(),所以实际上你有一个 4-way 冲突。

C 中的函数存在于整个项目的全局命名空间中。

从项目中删除所有没有您实际想要的 main() 版本的文件,然后重新编译。- 或将其他文件中的函数重命名为不同于main(). 您可能会收到警告说这些函数从未使用过,但项目将编译。


推荐阅读