【问题标题】:Why is the c++ standard library not working?为什么 c++ 标准库不起作用?
【发布时间】:2013-04-20 04:57:03
【问题描述】:

我一直在尝试让我从学校服务器下载的程序在我的 Mac 上离线运行。我尝试按照教程更新 GCC,但由于某种原因,即使我使用了给出的命令,这些教程也无法正常工作。

现在当我编译时..我收到一个错误说找不到..我不明白。我已经更新了 Xcode.. 遵循了大量的教程.. 我仍然无法运行它!

为什么说random is not found,导致致命错误?

谢谢

错误:

DungeonLevel.h:6:10: fatal error: 'random' file not found

“由于这是一个编码站点,我需要提供代码,因为我可能忘记包含头文件......”

#ifndef _DungeonLevel_included_
#define _DungeonLevel_included_

#include "Tile.h"
#include <vector>
#include <random>

class Player;

class DungeonLevel {
public:
    DungeonLevel(int iWidth, int iHeight, std::mt19937 & randomGen);
    ~DungeonLevel(void);

    void dump();
    char at(int x, int y);

    Creature * removeCreature(Creature * creatureToRemove);

    void moveCreature(Creature * creatureToMove, char dir);
    void placeInGame(Creature * creatureToPlace, std::mt19937 & randomGen);
    void placeInGame(Creature & creatureToPlace, std::mt19937 & randomGen);
    Tile & returnTile(int x,int y);
    int getWidth();
    int getHeight();

private:
    std::vector<std::vector<Tile>> m_vvTiles; //Tile was char

};

#endif

这是我的生成文件:

OBJECTS = Ammunition.o Armor.o Consumable.o Creature.o Entity.o Gold.o Item.o parser.o Potion.o Scroll.o Weapon.o XMLSerializable.o CreatureFactory.o DungeonLevel.o Player.o Tile.o ItemFactory.o
HEADERS = Ammunition.h Armor.h Consumable.h Creature.h Entity.h Gold.h Item.h parser.h Potion.h Scroll.h Weapon.h XMLSerializable.h CreatureFactory.h DungeonLevel.h Player.h Tile.h ItemFactory.h

all: Jhack

%.o: %.cpp $(HEADERS)
    clang++ -c $< -o $@ -std=c++11 -stdlib=libc++

Jhack: $(OBJECTS) main.o
    clang++ -o Jhack $^ -stdlib=libc++

clean:
    rm -f *.o Jhack

run: Jhack
    ./Jhack

【问题讨论】:

  • 大声笑,我显然没有忘记包含。同样的代码通过 SSH 也能正常工作。但我还是会发布代码
  • 如果我们知道它报告丢失了哪个文件,那就太好了。
  • 有 83 个文件。它包含在其中的几个中。第一次遇到...致命错误
  • 我认为查看您遇到的 full、actual 错误会很有帮助。
  • &lt;random&gt; 是在 C++11 中引入的。您使用的是旧版本的 GCC,或者您没有启用 C++11 模式。或者您的安装或文件系统已损坏。

标签: c++ macos gcc terminal standard-library


【解决方案1】:

Apple 的 gcc 确实过时了。尝试使用 clang 和 libc++ 而不是 gcc 和 libstdc++ 构建。编译标志:-std=c++11 -stdlib=libc++,链接标志:-stdlib=libc++。使用 clang++ 而不是 g++。

编辑:请注意,您需要安装最新的命令行工具才能使其工作。
打开 XCode。转到“Xcode”->“首选项...”->“下载”选项卡。选择“命令行工具”并安装它们。如果显示已安装 - 单击“立即检查并安装”按钮检查更新。
在终端中输入 clang++ --version 之后,您应该会看到类似下一个:

Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.3.0
Thread model: posix

编辑 2:如果这没有帮助并且您仍然拥有过时的编译器版本。尝试在您的 makefile 中使用 xcrun clang++ 而不是 clang++。这将使用 xcode 工具链。

【讨论】:

  • 是的,它也无法正常工作。呃,这太令人沮丧了。有没有办法让它使用安装在我文件中某处的新版本?
  • 您需要更新 XCode,从 XCode 安装命令行工具,在您的 makefile 中将 g++ 替换为 clang++,将我上面告诉过的编译器和链接器标志添加到 makefile 中的适当位置,make clean 和make
  • 看起来你没有添加-stdlib=libc++ 来编译标志
  • 我以前从未使用过标志。对不起,我可能让你对我缺乏知识感到沮丧=/
  • 将你的makefile中的这一行:g++ -c $&lt; -o $@ -std=c++0x替换为这一行:clang++ -c $&lt; -o $@ -std=c++11 -stdlib=libc++;这个:g++ -o Jhack $^ 和这个:clang++ -o Jhack $^ -stdlib=libc++
猜你喜欢
  • 2017-08-03
  • 2012-05-29
  • 2023-01-26
  • 1970-01-01
  • 2014-06-28
  • 2011-11-16
  • 2011-07-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多