简体   繁体   中英

How can I get my makefile to compile/link older and newer GCC/GNU libraries? C++

I have GNU 4.7.1 as the default on my MacBook. I keep getting tons of segfaults when I run my program on my school's server. I downloaded the current state of my program from my schools server so that I could use NetBeans to help with debugging. I've run into tons of problems getting the newer GNU to replace the ancient 4.2 version that my MacBook was continuing to set as the default.

The OSX compilers are completely new to me and I can't get my makefile adjusted to compile everything like it is on my school's server.

People have told me to use gcc or g++ instead of the other, but others have told me that it shouldn't matter. From what I understand, OSX uses different libraries for the newer versions of GNU for c/c++ and now Clang is the standard? I'd like to stick with GNU/GCC just because that's what I've used in my CS classes.

Currently it appears that when my makefile tries to link "std::mt19937" it can't find the header that links it.

My question is : how can I get my makefile to compile so that std::mt19937 and the c++11 libraries both are used?

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

# I tried this and adding $(LIBS) where "-std=c++0x" is below.. 
# LIBS = -std=c++0x -std=c++11 -std=gnu++11

%.o: %.cpp $(HEADERS)
    gcc -c $< -o $@ -std=c++0x

Jhack: $(OBJECTS) main.o
    gcc -o Jhack $^

clean:
        rm -f *.o Jhack

run: Jhack
    ./Jhack

I tried swapping out "-std=c++0x" for -std=c++11 and -std=gnu++11. I also tried adding them both. I also tried swapping gcc out with g++ but everything I seem to try or change causes more errors and warnings.

Here's the first part of the current error message (It goes on and on and on):

gcc -c Ammunition.cpp -o Ammunition.o -std=c++0x
gcc -c Armor.cpp -o Armor.o -std=c++0x
gcc -c Consumable.cpp -o Consumable.o -std=c++0x
gcc -c Creature.cpp -o Creature.o -std=c++0x
gcc -c Entity.cpp -o Entity.o -std=c++0x
gcc -c Gold.cpp -o Gold.o -std=c++0x
gcc -c Item.cpp -o Item.o -std=c++0x
gcc -c parser.cpp -o parser.o -std=c++0x
gcc -c Potion.cpp -o Potion.o -std=c++0x
gcc -c Scroll.cpp -o Scroll.o -std=c++0x
gcc -c Weapon.cpp -o Weapon.o -std=c++0x
gcc -c XMLSerializable.cpp -o XMLSerializable.o -std=c++0x
gcc -c CreatureFactory.cpp -o CreatureFactory.o -std=c++0x
gcc -c DungeonLevel.cpp -o DungeonLevel.o -std=c++0x
gcc -c Player.cpp -o Player.o -std=c++0x
gcc -c Tile.cpp -o Tile.o -std=c++0x
gcc -c ItemFactory.cpp -o ItemFactory.o -std=c++0x
gcc -c main.cpp -o main.o -std=c++0x
gcc -o Jhack 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 main.o
ld: warning: ignoring file Creature.o, file was built for unsupported file format ( 0x7f      0x45 0x4c 0x46 0x 2 0x 1 0x 1 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 0x 0 ) which is not the      architecture being linked (x86_64): Creature.o
Undefined symbols for architecture x86_64:
  "Creature::removeMonster(DungeonLevel&)", referenced from:
      vtable for Player in Player.o
  "Creature::dumpObjectData()", referenced from:
      Player::dumpObjectData()      in Player.o
  "Creature::setElementData(std::basic_string<char, std::char_traits<char>,     std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char>     >)", referenced from:
      Player::setElementData(std::basic_string<char, std::char_traits<char>,     std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char>   >) in Player.o
  "Creature::writeDataAsFragment(std::basic_ostream<char, std::char_traits<char> >&)",    referenced from:
      Player::writeDataAsFragment(std::basic_ostream<char, std::char_traits<char> >&) in     Player.o
  "Creature::move(DungeonLevel&, Creature&, std::mersenne_twister_engine<unsigned int,   32ul, 624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u,   18ul, 1812433253u>&)", referenced from:
      vtable for Player in Player.o
  "Creature::getHP()", referenced from:
      vtable for Player in Player.o
      _main in main.o
  "Creature::setHP(int)", referenced from:
      Player::Player() in Player.o
      Player::Player() in Player.o
      vtable for Player in Player.o
  "Creature::attack(Creature*, Creature&, std::mersenne_twister_engine<unsigned int, 32ul,   624ul, 397ul, 31ul, 2567483615u, 11ul, 4294967295u, 7ul, 2636928640u, 15ul, 4022730752u, 18ul,  1812433253u>&, DungeonLevel&)", referenced from:
      vtable for Player in Player.o
  "Creature::getXLoc()", referenced from:
      vtable for Player in Player.o

Turns out I messed up the path for my compiler and MacPorts installed to the wrong location? Fresh OSX install and proper GNU undated solves it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM