【问题标题】:(.bss+0x0): multiple definition of Proxies(.bss+0x0):代理的多重定义
【发布时间】:2012-09-12 17:25:32
【问题描述】:

我有这段代码用于自动化机器人。它使用 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 autonavigate navigation.o autonavigation.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): `gFrequency' 的多重定义 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);
}

【问题讨论】:

    标签: c++


    【解决方案1】:

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

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

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

    extern char hostname[32];
    

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

    char hostname[32];
    

    【讨论】:

    • 感谢 Joachim,它抱怨的这些名称甚至不在我的代码中。代码在下面 //navigation.h **/ #include #include #include #include "args.h" #define PI 3.14159 using namespace std ;使用命名空间 PlayerCc;类导航{公共:导航(); void autoNavigate(PlayerClient &, LaserProxy &, Position2dProxy &, PtzProxy &, IrProxy &, SonarProxy &);私人:无效墙跟随(LaserProxy &,Position2dProxy &); void barrierAvoid(IrProxy &, SonarProxy &, PlayerClient &, Position2dProxy &); };
    • //main (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);导航导航; nav.autoNavigate(机器人, lp, pp, ptp, ir, sp); }
    • 解决方案:我通过将“args.h”从 navigation.h 移动到 main (autonavigation.cpp) 来让它工作,因为它被包含了两次:直接在 navigation.h 和 autonavigation.cpp间接通过包含navigation.h。代理类在 main (autonavigation.cpp) 中定义,因此只需要在其中包含“args.h”标头。
    • @elefante 这似乎不正确,包含文件应该只包含一次,因为它只有一个 #include 指令。但是,它指出错误可能在该头文件中。顺便说一句,如果你想确保不包含多次头文件,你应该添加include guards
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    • 2020-05-18
    • 1970-01-01
    相关资源
    最近更新 更多