我有用于使机器人自动化的代码。它使用了Player的一些代理类-Player,它是用于编程机器人的开源软件。我使用以下命令编译了两个cpp文件:
g ++ -c -Wall navigation.cpp`pkg-config --cflags playerc ++``pkg-config --libs playerc ++`和g ++ -c -Wall autonavigation.cpp`pkg-config --cflags playerc ++``pkg-config --libs playerc ++`然后,我链接了目标文件(这就是问题所在):g ++ -o自动导航navigation.o autonavigation.o`pkg-config --cflags playerc ++``pkg-config --libs playerc ++`。
代码在这里:
erratic @ erratic-desktop:〜/ Desktop / autonav $ g ++ -o自动导航navigation.oautonavigation.o`pkg-config --cflags playerc ++``pkg-config --libs playerc ++`autonavigation.o :(。bss + 0x0):'gHostname'的多个定义navigation.o :(。bss + 0x0):首先在这里定义autonavigation.o :(。data + 0x0):``gPort''的多个定义navigation.o :(。data + 0x0):首先在这里定义autonavigation.o :(。bss + 0x4):gIndex的多个定义navigation.o :(。bss + 0x4):首先在这里定义autonavigation.o :(。bss + 0x8):gDebug的多个定义navigation.o :(。bss + 0x8):首先在这里定义autonavigation.o :(。data + 0x4):gRFrequency的多个定义navigation.o :(。data + 0x4):首先在这里定义autonavigation.o :(。data + 0x8):gDataMode的多个定义navigation.o :(。data + 0x8):首先在这里定义autonavigation.o :(。bss + 0xc):gUseLaser的多个定义navigation.o :(。bss + 0xc):首先在这里定义autonavigation.o:在函数`parse_args(int,char **)'中:autonavigation.cpp :(。text + 0x0):`parse_args(int,char **)'的多个定义navigation.o:navigation.cpp :(。text + 0x0):首先在这里定义autonavigation.o:在函数`print_usage(int,char **)'中:autonavigation.cpp :(。text + 0x101):`print_usage(int,char **)'的多个定义navigation.o:navigation.cpp :(。text + 0x101):首先在这里定义collect2:ld返回1退出状态
来自注释的源代码:
//navigation.h
#include <libplayerc++/playerc++.h>
#include <stdio.h>
#include <time.h>
#include "args.h"
#define PI 3.14159
using namespace std;
using namespace PlayerCc;
class navigation
{
public:
navigation();
void autoNavigate(PlayerClient &, LaserProxy &, Position2dProxy &, PtzProxy &, IrProxy &, SonarProxy &);
private:
void wallFollow(LaserProxy &, Position2dProxy &);
void obstacleAvoid(IrProxy &, SonarProxy &, PlayerClient &, Position2dProxy &);
};
autonavigation.cpp
的来源:
#include "navigation.h"
int main(int argc, char *argv[])
{
PlayerClient robot("localhost");
LaserProxy lp(&robot,0);
Position2dProxy pp(&robot,0);
PtzProxy ptp (&robot,0);
IrProxy ir(&robot,0);
SonarProxy sp(&robot, gIndex);
navigation nav;
nav.autoNavigate(robot, lp, pp, ptp, ir, sp);
}
没有看到您的代码,我们只能猜测,但是我的猜测是您define在两个源文件中都包含的头文件中这些变量。
您应该声明变量,并使用extern
告诉编译器变量在其他地方定义。然后在one源文件中定义变量(即与头文件中的声明相同,但没有extern
关键字)。
例如,假设我有一个要在多个源文件中使用的变量hostname
,然后在一个头文件中进行了一个extern
声明,我将其包含在所有需要该变量的源文件中:
extern char hostname[32];
然后在一个源文件中定义变量:
char hostname[32];