c - 如何解决“'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)
使用它的平台:
解决方案
这是一个项目管理问题。IDE 显示您有 4 个文件,所有这些文件都相互冲突。你有t1.c
, t2.c
,t3.c
和main.c
. 他们都试图定义main()
,所以实际上你有一个 4-way 冲突。
C 中的函数存在于整个项目的全局命名空间中。
从项目中删除所有没有您实际想要的 main() 版本的文件,然后重新编译。- 或将其他文件中的函数重命名为不同于main()
. 您可能会收到警告说这些函数从未使用过,但项目将编译。
推荐阅读
- spring-security - Spring Security 中的 hasRole() 和 hasAuthority() 有什么区别
- c# - 页面加载后修改viewbag内容
- python - 无法打开之前打开的 H5
- video-streaming - 带有 iframe 信息的实时 HLS 生成器
- postgresql - 如何正确分组 Postgres 数据
- pandas - 以 pandas 列作为参数调用函数
- excel - VBA - 填充自定义功能区下拉/列表框
- python - 'requests' 导入的模块在 IDLE 上不起作用
- angular - 如何强制 this.gridOption.getRowStyle 函数?
- python - 从表格中排除一些结果-Web Scraping with Python