gpt4 book ai didi

c++ - (.bss+0x0) : multiple definition of Proxies

转载 作者:太空狗 更新时间:2023-10-29 21:04:00 25 4
gpt4 key购买 nike

我有这段用于自动化机器人的代码。它使用来自 Player 的一些代理类 - 一种用于编程机器人的开源软件。我使用这些命令编译了两个 cpp 文件:

g++ -c -Wall  navigation.cpp `pkg-config --cflags playerc++` `pkg-config --libs playerc++`andg++ -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 hereautonavigation.o:(.data+0x0): multiple definition of `gPort'navigation.o:(.data+0x0): first defined hereautonavigation.o:(.bss+0x4): multiple definition of `gIndex'navigation.o:(.bss+0x4): first defined hereautonavigation.o:(.bss+0x8): multiple definition of `gDebug'navigation.o:(.bss+0x8): first defined hereautonavigation.o:(.data+0x4): multiple definition of `gFrequency'navigation.o:(.data+0x4): first defined hereautonavigation.o:(.data+0x8): multiple definition of `gDataMode'navigation.o:(.data+0x8): first defined hereautonavigation.o:(.bss+0xc): multiple definition of `gUseLaser'navigation.o:(.bss+0xc): first defined hereautonavigation.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 hereautonavigation.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 herecollect2: 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 &);
};

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);
}

最佳答案

在没有看到您的代码的情况下,我们只能猜测,但我的猜测是您在两个源文件中都包含的头文件中定义这些变量。

您应该声明 变量,并使用extern 告诉编译器变量在别处定义。然后在 一个 源文件中定义变量(即与头文件中的声明相同,但没有 extern 关键字)。

例如,假设我有一个要在多个源文件中使用的变量 hostname,然后我在包含的头文件中做了一个 extern 声明在所有需要变量的源文件中:

extern char hostname[32];

然后在一个源文件中我定义了变量:

char hostname[32];

关于c++ - (.bss+0x0) : multiple definition of Proxies,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511044/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com