【发布时间】:2021-11-29 21:08:46
【问题描述】:
我有一个头文件command.h,其中包含我所有的变量和函数声明
//command.h
int someVar1;
int someVar2;
void modifying_loop (int a, int b);
int someVar3;
.
.
.
在另一个文件my_algorithm.c中我定义了之前声明的函数modifying_loop并使用了一些在头文件中声明的变量
//my_algorithm.c
#include "command.h"
void modifying_loop (int x, int y)
{
someVar1 = x+2;
someVar2 = y+2;
}
我有我的主文件command.c 我这样调用modifying_loop 函数:
#include "command.h"
int main ()
{
modifying_loop(5,6);
return 0;
}
我使用返回我的gcc -o command command.c -lm -lpigpio -L/usr/lib/ 编译它
undefined reference to modifying_loop'
然后解决我使用链接my_algorithm.c文件
gcc -o command command.c my_algorithm.c -lm -lpigpio -L/usr/lib/ 这给了我以下信息:
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c18): multiple definition of `someVar1'; /tmp/ccaydPyq.o:(.bss+0x24918): first defined here
/usr/bin/ld: /tmp/cc6ad5oo.o:(.bss+0x3c1c): multiple definition of `someVar2'; /tmp/ccaydPyq.o:(.bss+0x2491c): first defined here
以及头文件中声明的其余变量的相同错误。有谁知道是什么导致了错误。
【问题讨论】:
-
someVar1和someVar2是干什么用的?您可能需要在 command.h 中将它们声明为extern,然后在其中一个 .c 文件中定义它们。但是它们是全局变量,这通常是个坏主意。 -
"函数在头文件中定义并在另一个c文件中声明" --> 我想你希望"函数在头文件中声明并且定义 在另一个 c 文件中”。
-
请更新问题标题以反映真正的问题,即“多重定义”问题,因为您已经有了解决“未定义引用”问题的正确方法。
-
在.h文件中,
void modifying_loop (int a, int b)之后,有';'或'{"还是什么?
标签: c