0

I am using a Makefile to compile a C++ project, and I receive an undefined reference error which I suspect is for a simple mistake.

The error itself is:

$ make
g++ -c main.cpp
g++ -o p5 main.o
main.o:main.cpp:(.text+0x241): undefined reference to `Instructions::processInput(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
Makefile:2: recipe for target `p5' failed
make: *** [p5] Error 1

Here are the parts of the project concerned with the error (for clarity's sake): My makefile:

p5: main.o Instructions.o
    g++ -o p5 main.o

main.o: main.cpp Instructions.h
    g++ -c main.cpp

Instructions.o: Instructions.h Instructions.cpp
    g++ -c Instructions.cpp

My main.cpp file:

#include <string>
#include "Instructions.h"
using namespace std;

int main() {
    Instructions inst;
    inst.processInput("some string par example"); //THIS LINE HERE GIVES ME ERRORS
    return 0;
}

My Instructions header file:

#ifndef INSTRUCTIONS_H
#define INSTRUCTIONS_H
#include <string>

class Instructions {
public:
    Instructions() {input = ""; command = 0; xCoord = 0.0; yCoord = 0.0;};
    void processInput(std::string in);
private:
    std::string input;
    int command;
    double xCoord;
    double yCoord;
};
#endif

And finally the .cpp file, very barebones at the moment:

#include "Instructions.h"
#include <string>
#include <iostream>
using namespace std;

void Instructions::processInput(string in) {
    cout << "Processing: " << input << endl;
}

I've been looking around for solutions but to no avail. Please excuse me then if it is indeed somewhere else! I also hope it helps all the beginners out there still coming to terms with C++!

Løiten
  • 3,185
  • 4
  • 24
  • 36
Eamon Bohan
  • 522
  • 2
  • 8
  • 22
  • maybe it thinks that "some string par example" means a char* instead of a string, so what you are really calling is processInput(char*) instead of processInput(string). In this case, try doing processInput(string("helloimastring")) – Name Dec 02 '12 at 01:46
  • Hey, @ForgiveMeI'mAN00b, I tried that and while it should compile, it gives me the same error. I assume this is because while corrrect syntax, the problem is the same. – Eamon Bohan Dec 02 '12 at 01:51
  • odd, sorry though that's the best help i can do, I don't quite grasp all the stuff to do with compilation yet myself... :| – Name Dec 02 '12 at 02:03

1 Answers1

2

try this please Makefile :

 p5: Instructions.o main.o
     g++ Instructions.o main.o -o p5 

 Instructions.o: Instructions.cpp
     g++ -c Instructions.cpp -o Instructions.o 

 main.o: main.cpp Instructions.h
     g++ -c main.cpp Instructions.o -o main.o

to compile p5, you need first to compile all its dependencies which is Instructions.o and main.o. Instructions.o is independent, so it can be compiled like this g++ -c Instructions.cpp. But main.o depend on the class Instructions so it depend on its .o It should be compiled like this g++ -c main.cpp Instructions.o.

Same thing for p5, it needs all the *.o.

  • Hi! Unfortunately this doesn't work either, but it raises a doubts on my knowledge of makefiles. Does then, the .o file have to be fed instead of the .header one when making a new .o one? – Eamon Bohan Dec 02 '12 at 01:54
  • how about this ? I added `Instructions.o` to the end ...check plz –  Dec 02 '12 at 01:55
  • Hmmm, it throws the same compiler error. Thanks for the help though, I appreciate it! – Eamon Bohan Dec 02 '12 at 01:59
  • I modified the makefile, plz check if the error msg changed ? –  Dec 02 '12 at 02:00
  • Hey, that seems to have worked! What does this mean? The compiler needs a specific order of file making? Main files towards the end? – Eamon Bohan Dec 02 '12 at 02:05
  • okey, first I need you to check my answer as Right and I will edit it right now :) –  Dec 02 '12 at 02:06