3

I've been trying to get my program that I downloaded from my schools server to run offline on my mac. I tried updating GCC by following tutorials and for some reason the tutorials didn't work even though I was using the commands given.

Now when I compile.. I get an error saying that is not found.. I don't get it. I've updated Xcode.. followed tons of tutorials.. and I still can't get the thing to run!

Why is it saying that random is not found, causing a fatal error?

Thanks

Error:

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

"Since this is a coding site, I need to provide code because I probably forgot to include a header file..."

#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

Here's my makefile:

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
JGeis
  • 31
  • 1
  • 2
  • 6
  • Lol I obviously didn't forget an includes.. And the same code works fine via SSH.. But I'll post code anyways – JGeis Apr 26 '13 at 05:27
  • It would be nice if we knew which file it reports as missing. – Captain Obvlious Apr 26 '13 at 05:28
  • There are 83 files. It's included in several of them. The first time it encounters it... Fatal error – JGeis Apr 26 '13 at 05:30
  • 1
    I think it would help a lot to see the _full_, _actual_ error that you are encountering. – Nbr44 Apr 26 '13 at 05:33
  • Lol I literally said exactly what the error said. But okay, I'll copy and paste it. – JGeis Apr 26 '13 at 05:34
  • 2
    `` was introduced in C++11. Either you're using an old version of GCC or you don't have C++11 mode enabled. Or your installation or file system is corrupt. – Captain Obvlious Apr 26 '13 at 05:34
  • @CaptainObvlious that's possible. I tried the MacPorts tutorials and the commands it showed to run weren't working. I can't seem to get it to update the newer GCC. – JGeis Apr 26 '13 at 05:36
  • What do you get when you type `which g++` and `g++ --version` in the console? – juanchopanza Apr 26 '13 at 05:49
  • @juanchopanza I can't get it to update. I tried following tutorials and the commands they tell me to enter don't work. It's at 4.2.1 currently :/ – JGeis Apr 26 '13 at 05:50
  • Usually, macports installs go to `/opt/local` or something. You could try there, or try with an explicit version, e.g `g++4.7` or something. I am not on a mac OS at the moment so I can't check the exact details, but you get the general idea: mac needs to keep its system versions of GCC stable, but you can install any number of other versions. You just have to figure out how to pick those up. – juanchopanza Apr 26 '13 at 05:57
  • Uhhh how do I do that? I see them in finder when I search for the library files. – JGeis Apr 26 '13 at 06:00
  • I don't know much about Mac, but does this help: http://stackoverflow.com/questions/14153725/installing-gcc-4-7-1-on-os-x Can your question perhaps even be regarded as a duplicate of that question? – jogojapan Apr 26 '13 at 06:14

1 Answers1

10

Apple's gcc is really outdated. Try to build using clang and libc++ instead of gcc and libstdc++. Compile flags: -std=c++11 -stdlib=libc++, link flag: -stdlib=libc++. Use clang++ instead of g++.

Edit: note that you need to install latest command line tools for this to work.
Open XCode. Go to "Xcode" -> "Preferences..." -> "Downloads" tab. Select "Command Line Tools" and install them. If it says that it is installed - check for updates by clicking on "Check and Install Now" button.
After that type clang++ --version in terminal and you should see something like next:

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

Edit 2: if that didn't help and you still have an outdated version of compiler. Try to use xcrun clang++ instead of clang++ in your makefile. That will use xcode toolchain.

cody
  • 3,233
  • 1
  • 22
  • 25
  • 1
    Yeah can't get that to work either. Ugh this is so frustrating. Is there not a way to get it to use the new version that's installed somewhere in my files? – JGeis Apr 26 '13 at 06:13
  • You need to update XCode, install command line tools from XCode, replace g++ with clang++ in your makefile, add compiler and linker flags I've told above to the appropriate places in makefile, `make clean` and `make` – cody Apr 26 '13 at 06:21
  • Looks like you didn't add `-stdlib=libc++` to compile flags – cody Apr 26 '13 at 06:24
  • I've never used flags before. Sorry I'm probably making you just as frustrated with my lack of knowledge =/ – JGeis Apr 26 '13 at 06:27
  • Replace this line in your makefile: `g++ -c $< -o $@ -std=c++0x` with this one: `clang++ -c $< -o $@ -std=c++11 -stdlib=libc++`; this one: `g++ -o Jhack $^` with this one: `clang++ -o Jhack $^ -stdlib=libc++` – cody Apr 26 '13 at 06:31
  • clang++ -c Creature.cpp -o Creature.o -std=c++11 -stdlib=libc++ error: invalid value 'c++11' in '-std=c++11' make: *** [Creature.o] Error 1 – JGeis Apr 26 '13 at 06:37
  • what `clang++ --version` tells you? I've just tested it on my mac - no errors. – cody Apr 26 '13 at 06:39
  • Apple clang version 2.1 (tags/Apple/clang-163.7.1) (based on LLVM 3.0svn) Target: x86_64-apple-darwin12.3.0 Thread model: posix – JGeis Apr 26 '13 at 06:42
  • WTF? What version of OS X are you using and what version of XCode? – cody Apr 26 '13 at 06:42
  • I've updated my answer. You should update command line tools. – cody Apr 26 '13 at 06:48
  • You probs don't believe me, but it says it's up to date. – JGeis Apr 26 '13 at 06:55
  • And what about button "Check and Install Now"? And I suggest you to enable checkbox "Check for and install updates automatically" to avoid such problems in future. – cody Apr 26 '13 at 06:56
  • No, simulators wouldn't help you. Lets try something else. Run `xcrun -find clang++` in terminal. What would it say to you? – cody Apr 26 '13 at 07:04
  • /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ – JGeis Apr 26 '13 at 07:05
  • ok. Now replace all entries of `clang++` in your makefile with `xcrun clang++` – cody Apr 26 '13 at 07:07
  • That means that it finally started to compile. You can ignore warnings (but I usually prefer to fix them). Do you have any errors now? – cody Apr 26 '13 at 07:12
  • You are welcome. clang has many warnings flags and as far as I know most of them are enabled by default. And off course you can ask author of the code about the warnings ;) – cody Apr 26 '13 at 07:17
  • OK cool. I did a make clean and now its saying No targets specified and no makefile found. Stop. – JGeis Apr 26 '13 at 07:20
  • `make clean` will just remove all object files and a binary. To build it again you need to run `make` again. Or this what you are getting when you are running `make` again? If so check if there is a Makefile out there in current directory. – cody Apr 26 '13 at 07:24
  • Yeah that's what I got from make again. I reverted though. Somehow the makefile must have gotten deleted. Anyways thanks a ton man! – JGeis Apr 26 '13 at 07:25