c++ - 为什么 C++ 标准库不起作用?

标签 c++ macos gcc terminal standard-library

我一直在尝试让我从学校服务器下载的程序在我的 Mac 上离线运行。我尝试按照教程更新 GCC,但出于某种原因,即使我使用给定的命令,教程也不起作用。

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

为什么说没有找到random,导致 fatal error ?

谢谢

错误:

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

最佳答案

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 工具链。

关于c++ - 为什么 C++ 标准库不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16229297/

相关文章:

c - gcc如何判断默认生成32位还是64位的可执行文件?

c++ - 运算符优先级与计算顺序

请提供 C++ 线程和字符串帮助

macos - osx下imagemagick的导入

swift - 从函数中打开 NSViewController。菜单栏应用

c++ - C++中的SSE4内联汇编问题

c++ - 对于需要全局范围的固定数量的对象,堆栈与堆

c++ - 结构体、数组和函数 C++

xcode - 使用 Swift 为 OS X 应用程序动画 View 移动

xcode - 从 <math.h> 链接函数时,OSX/Darwin 无法解析符号