- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个工作区下创建了一个简单的文件,构建并成功执行。并再次在同一工作区下创建另一个文件。我收到链接错误,例如:
"====Linking===== Error: Duplicate public_main in module...."
在构建时,当我尝试执行第一个文件时,我也遇到了相同的错误。
请查看下面的代码并提出建议。
文件1:n_numbers.c
#define N 10
main()
{
int count;
float sum, number;
count=0;
sum=0;
printf("Enter 10 numbers to calculate the Sum");
while(N<10)
{
scanf("%f", &number);
sum=sum+number;
count=count+1;
}
printf("sum=5.2%", sum);
}
文件2:two.numbers.c
main()
{
int a,b,c;
printf("Enter Two numbers for a and b\n");
scanf("%d %d",&a, &b);
c=a+b;
printf("c=%d", c);
}
最佳答案
我怀疑你的问题是:
gcc file_a.c
a.out
作品
gcc file_b.c
a.out
作品
gcc file_a.c file_b.c
linker error
因为 gcc 正在尝试创建单个可执行文件,并且每个可执行文件必须恰好有一个 main
函数。
如果 file_a 和 file_b 都定义了一个名为 public_main
的函数,那么问题就变成了在任何给定的调用中要使用哪个函数 - C 通过一个简单的规则解决了这个问题 - 每个可执行文件中的每个函数只能一个。。
注意其他语言有不同的规则,C++ 可以有多个具有相同名称但不同原型(prototype)的函数(重载),或者在不同的类(命名空间)内,它实际上通过所谓的名称修改来实现这两个功能。 Python 使用命名空间和复杂的作用域规则等。
Hi Steve, I had only one main() in each executable, but still i am getting same error – user2714972
You have 2 .c files and you are linking them into a single executable, (.exe), that is what gcc does when you give it more than one .c file if you would like to make 2 executables you need to call gcc twice, once with each .c and with -o differcent_exe_name to stop both being called a.out or a.exe – Steve Barnes
如果正如某些人所建议的那样,您正在使用 IDE,则需要创建单独的项目或根据 IDE 指定单独的目标。
关于c - "Linking Error: Duplicate public_main in module"构建时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18426621/
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是一名优秀的程序员,十分优秀!