Reputation: 105
I have this code for automating a robot. It uses some proxy classes from Player - an open-source software for programming robots. I compiled the two cpp files using these commands:
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++`.
The code is here:
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
Source code from comment:
//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 &);
};
Source for 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);
}
Upvotes: 5
Views: 11308
Reputation: 409166
Without seeing your code we can only guess, but my guess is that you define these variables in a header file that you include in both source files.
You should declare the variables, and be using extern
to tell the compiler that the variables are defined somewhere else. Then in one source file you define the variables (i.e. the same as the declarations in the header file but without the extern
keyword).
For example, lets say I have a variable hostname
that I want to use in multiple source files, then I make an extern
declaration in a header file that I include in all source files that needs the variable:
extern char hostname[32];
Then in one source file I define the variable:
char hostname[32];
Upvotes: 7