Niek de Klein
Niek de Klein

Reputation: 8824

Error when compiling c++ file: Undefined symbols

I'm trying to compile someone else's C++ code. I have 0 experience with C++ myself. I'm using g++ on a MAC to compile one of the .cpp files that I received. When I do g++ main.cpp I get an Undefined symbols error. When googling the answers seem to be about a wrong link between files, but I don't know how to link files. How can I get the file to compile? I pasted the full code below.

Undefined symbols:
  "initializeFitness()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::GetNeighbourhood(int, int, std::vector<Agent**, std::allocator<Agent**> >&)", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Grid::Grid()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Reaper::GetAgentToKill()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Cupid::GetRandomBreeder()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Reaper::Select()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::~Grid()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Breeder::Breed(Agent**, Agent**)", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Cupid::Select()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Cupid::GetEmptyCell()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Agent::GetType()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Breeder::ProcessNeighbourhood(std::vector<Agent**, std::allocator<Agent**> > const&)", referenced from:
      runEvolution()     in ccZXBTDH.o
  "FateAgent::GetGenome(double*)", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Cupid::GetParents()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Agent::IncreaseAge()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::GetAgent(int, int)", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "FateAgent::GetSelectedCount()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Breeder::GetGenome(double*)", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Agent::GetFitness()", referenced from:
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
      runEvolution()     in ccZXBTDH.o
  "Agent::~Agent()", referenced from:
      runEvolution()     in ccZXBTDH.o
  "Grid::DoMovement()", referenced from:
      runEvolution()     in ccZXBTDH.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Upvotes: 0

Views: 3166

Answers (5)

Shane Hsu
Shane Hsu

Reputation: 8357

I don't think that is the code, I think that is debug output.

runEvolution() uses functions that hasn't been defined yet. Look for definitions of those.

Like a lot of people suggested, You may be missing some files, gcc clearly miss something in your code.

Good luck!

Upvotes: 1

none
none

Reputation: 12087

assuming the program is not using any library you should compile all files with -c switch and then link them altogether with:

g++ -c main.cpp
g++ -c file1.cpp
..
g++ *.o

or you can automatically do the same with:

g++ *.cpp

Upvotes: 1

alanxz
alanxz

Reputation: 2026

g++ cannot find a bunch of symbols, without any further knowledge of your situation - I would guess you're not compiling all of your source code files before linking.

Little more background:

When you call g++ just one source file argument it compiles the file, then tries to link it to a complete executable.

My recommendation is to compile each source file using the -c flag (the -c flag tells the compiler to compile the file down to object code, but not link it into an executable).

g++ -c main.cpp
g++ -c file1.cpp
g++ -c file2.cpp

The above commands will create object files with the .cpp replaced with .o. You can then link them all together by doing something like

g++ main.o file1.o file2.o

This will produce an executable a.out which you can run. This should help you with missing symbol linker errors.

If you're going to do anything more complicated than this I would highly suggest investigating in using a Makefile to build the code.

Upvotes: 4

tozka
tozka

Reputation: 3451

I can only guess here. How many files are in the project? Does it have any libraries?

If you have libraries you need to link them with -l switch

g++ -o output_binary_name source_file -lLibrary1 -lLibrary2

Or most likely you have more then one source file in the project, you will need to enumerate them

g++ -o output_binary_name source_file1 source_file2 source_file2

Upvotes: 2

tomahh
tomahh

Reputation: 13661

I think you are trying to compile only a part of the entire program. Try g++ *.cpp
If it still doesnt work, It may be because your program needs an external library.

Upvotes: 5

Related Questions