Why is the c++ standard library not working?

16,402

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.

Share:
16,402
JGeis
Author by

JGeis

Updated on June 04, 2022

Comments

  • JGeis
    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
    JGeis about 11 years
    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?
  • cody
    cody about 11 years
    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
    cody about 11 years
    Looks like you didn't add -stdlib=libc++ to compile flags
  • JGeis
    JGeis about 11 years
    I've never used flags before. Sorry I'm probably making you just as frustrated with my lack of knowledge =/
  • cody
    cody about 11 years
    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++
  • JGeis
    JGeis about 11 years
    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
  • cody
    cody about 11 years
    what clang++ --version tells you? I've just tested it on my mac - no errors.
  • JGeis
    JGeis about 11 years
    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
  • cody
    cody about 11 years
    WTF? What version of OS X are you using and what version of XCode?
  • cody
    cody about 11 years
    I've updated my answer. You should update command line tools.
  • JGeis
    JGeis about 11 years
    You probs don't believe me, but it says it's up to date.
  • cody
    cody about 11 years
    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
    cody about 11 years
    No, simulators wouldn't help you. Lets try something else. Run xcrun -find clang++ in terminal. What would it say to you?
  • JGeis
    JGeis about 11 years
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeD‌​efault.xctoolchain/u‌​sr/bin/clang++
  • cody
    cody about 11 years
    ok. Now replace all entries of clang++ in your makefile with xcrun clang++
  • cody
    cody about 11 years
    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
    cody about 11 years
    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 ;)
  • JGeis
    JGeis about 11 years
    OK cool. I did a make clean and now its saying No targets specified and no makefile found. Stop.
  • cody
    cody about 11 years
    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.
  • JGeis
    JGeis about 11 years
    Yeah that's what I got from make again. I reverted though. Somehow the makefile must have gotten deleted. Anyways thanks a ton man!