为什么我的C函数会出现“重复符号”错误?

问题描述 投票:0回答:1

我有三个C文件,main.cfunction1.cfunction2.c。我用gcc -o main main.c function1.c function2.c编译它们。我在helper()中定义了一个辅助函数function1.c。所以function1.c看起来像这样:

void helper();

void function() {
...
}

void helper() {
...
}

但是,当我尝试在helper()中以相同的方式定义function2.c时,出现错误“ duplicate symbol '_helper'”。我认为这是因为我已经一起编译了function1.cfunction2.c,所以function2.c知道helper()function1.c的定义,但是我无法在helper()中调用function2.c。我也不想在自己的文件中定义helper(),因为我最终可能会写很多这样的帮助文件。理想的情况是,如果我有一个helper.c文件,其中包含功能helper1()helper2()等,都可以从function1.cfunction2.c访问。有谁知道该怎么做,或者更好的选择?

c gcc helper helper-functions
1个回答
0
投票

无需两次实现相同的功能

只需在function1.c中实施

void helper() {
...
}

function2.c中仅声明它而不执行

extern void helper();
© www.soinside.com 2019 - 2024. All rights reserved.