我有三个C文件,main.c
,function1.c
和function2.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.c
和function2.c
,所以function2.c
知道helper()
中function1.c
的定义,但是我无法在helper()
中调用function2.c
。我也不想在自己的文件中定义helper()
,因为我最终可能会写很多这样的帮助文件。理想的情况是,如果我有一个helper.c
文件,其中包含功能helper1()
,helper2()
等,都可以从function1.c
和function2.c
访问。有谁知道该怎么做,或者更好的选择?
无需两次实现相同的功能
只需在function1.c
中实施
void helper() {
...
}
在function2.c
中仅声明它而不执行
extern void helper();