c++ - ld : duplicate symbols for architecture x86_64 when defining a global var in a header

标签 c++ macos duplicates x86-64 symbols

如何解决这个问题?

只是得到以下错误:

g++ 输出:

duplicate symbol _game in:
    /var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/main-62666f.o
    /var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/Game-a5a31d.o
duplicate symbol _game in:
    /var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/main-62666f.o
    /var/folders/w6/yqp0s0t92hl5g842hg8tztfr0000gn/T/Write-83f8ee.o

看来问题不在头文件中。

编辑:头文件如下所示:

#ifndef GAME_H
#define GAME_H
#include "Color.h"
#include "Tile.h"

class Game
{
  public:

    Game();
    ~Game();
    Color getActivePlayer();
    void setStarttile(Tile Firststarttile);
    Color togglePlayer();
    void setRunning(bool run);
    char newActiveplayer;
    void run();
    void runsecondmethod();

  private:
    Game(const Game &);
    Color Activeplayer;
    Tile *Starttile;
    bool Running;
}game;

#endif

最佳答案

您在头文件“Game.h”中实例化了一个Game 实例,由于它被包含在多个文件中,您最终会得到多个game 实例在链接时。变化:

class Game
{
  public:

  ...

}game;

到:

class Game
{
  public:

  ...

};

extern Game game;

然后添加:

Game game;

在您的一个 .cpp 文件中。

关于c++ - ld : duplicate symbols for architecture x86_64 when defining a global var in a header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30352884/

相关文章:

Mysql选择重复行之一

c++ - 关于用括号括起来的声明类型是什么的问题

c++ - 将成员函数作为参数传递给函数模板

macos - Mac 上的 EventKit 和应用程序扩展

bash - brew update 不工作——update.sh 上的错误替换错误

mysql - MYSQL 中的校验和选项分配给每个字段

c++ - 如何公开常量数据

c++ - 同一父类(super class)的两个类之间的相互更新

linux - Git : mysterious changes cannot be undone 的奇怪行为

c# - 以有效方式在 Designer 中复制完整表单(Visual C# Net 2)