我有这段用于自动化机器人的代码。它使用 Player 中的一些代理类 - Player 是一个用于机器人编程的开源软件。我使用以下命令编译了两个 cpp 文件:
g++ -c -Wall navigation.cpp `pkg-config --cflags playerc++` `pkg-config --libs playerc++`
and
g++ -c -Wall autonavigation.cpp `pkg-config --cflags playerc++` `pkg-config --libs playerc++`
I then linked the object files (which is where the problem arises):
g++ -o autonavigate navigation.o autonavigation.o `pkg-config --cflags playerc++` `pkg-config --libs playerc++`.
代码在这里:
erratic@erratic-desktop:~/Desktop/autonav$ g++ -o autonavigate navigation.o
autonavigation.o `pkg-config --cflags playerc++` `pkg-config --libs playerc++`
autonavigation.o:(.bss+0x0): multiple definition of `gHostname'
navigation.o:(.bss+0x0): first defined here
autonavigation.o:(.data+0x0): multiple definition of `gPort'
navigation.o:(.data+0x0): first defined here
autonavigation.o:(.bss+0x4): multiple definition of `gIndex'
navigation.o:(.bss+0x4): first defined here
autonavigation.o:(.bss+0x8): multiple definition of `gDebug'
navigation.o:(.bss+0x8): first defined here
autonavigation.o:(.data+0x4): multiple definition of `gFrequency'
navigation.o:(.data+0x4): first defined here
autonavigation.o:(.data+0x8): multiple definition of `gDataMode'
navigation.o:(.data+0x8): first defined here
autonavigation.o:(.bss+0xc): multiple definition of `gUseLaser'
navigation.o:(.bss+0xc): first defined here
autonavigation.o: In function `parse_args(int, char**)':
autonavigation.cpp:(.text+0x0): multiple definition of `parse_args(int, char**)'
navigation.o:navigation.cpp:(.text+0x0): first defined here
autonavigation.o: In function `print_usage(int, char**)':
autonavigation.cpp:(.text+0x101): multiple definition of `print_usage(int, char**)'
navigation.o:navigation.cpp:(.text+0x101): first defined here
collect2: ld returned 1 exit status
源码来自评论:
//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);
}