Why is the c++ standard library not working?
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.
JGeis
Updated on June 04, 2022Comments
-
JGeis almost 2 years
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 about 11 yearsYeah 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?
-
cody about 11 yearsYou 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
andmake
-
cody about 11 yearsLooks like you didn't add
-stdlib=libc++
to compile flags -
JGeis about 11 yearsI've never used flags before. Sorry I'm probably making you just as frustrated with my lack of knowledge =/
-
cody about 11 yearsReplace 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++
-
JGeis about 11 yearsclang++ -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
-
cody about 11 yearswhat
clang++ --version
tells you? I've just tested it on my mac - no errors. -
JGeis about 11 yearsApple 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
-
cody about 11 yearsWTF? What version of OS X are you using and what version of XCode?
-
cody about 11 yearsI've updated my answer. You should update command line tools.
-
JGeis about 11 yearsYou probs don't believe me, but it says it's up to date.
-
cody about 11 yearsAnd 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 about 11 yearsNo, simulators wouldn't help you. Lets try something else. Run
xcrun -find clang++
in terminal. What would it say to you? -
JGeis about 11 years/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++
-
cody about 11 yearsok. Now replace all entries of
clang++
in your makefile withxcrun clang++
-
cody about 11 yearsThat 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 about 11 yearsYou 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 ;)
-
JGeis about 11 yearsOK cool. I did a make clean and now its saying No targets specified and no makefile found. Stop.
-
cody about 11 years
make clean
will just remove all object files and a binary. To build it again you need to runmake
again. Or this what you are getting when you are runningmake
again? If so check if there is a Makefile out there in current directory. -
JGeis about 11 yearsYeah that's what I got from make again. I reverted though. Somehow the makefile must have gotten deleted. Anyways thanks a ton man!