I know this is probably an unbelievably simple error somewhere, but I can't see it. I keep getting "undefined reference to Machine::Machine()" and all of Machine's other functions.

main.cpp

#include "machine.h"

using namespace std;

int main(int argc, char** argv) {
   Machine* m = new Machine();
   m->reset();
   m->run();
   delete m;
   return 0;
}

machine.h

#ifndef _MACHINE_H_
#define _MACHINE_H_

#include "user.h"

class Machine {
public:
   Machine();
   ~Machine();

   void run();
   void reset();

private:
   User currentUser;
};

#endif

machine.cpp

#include "machine.h"

Machine::Machine() {
}

Machine::~Machine() {
}

void Machine::run() {
}

void Machine::reset() {
}

I know I'm going to kick myself once someone answers this. Thanks in advance.
-andrax

Dani AI

Generated

Short diagnostic and next steps (build + symbol checks) to complement 's Makefile tips and the reports from .

The problem shown — linker complaining the constructor and other Machine methods are undefined even after listing machine.cpp on the link line — means either (a) the object that should contain those symbols was never produced/linked, or (b) the definitions in the .cpp do not match the declarations (namespace/signature mismatch), or (c) stale/wrong object files are being used. The simplest way to tell which is true is to compile machine.cpp to an object and inspect that object for the constructor symbol.

Example diagnostics to run on a g++ toolchain:

g++ -c -Wall -g machine.cpp -o machine.o
nm -C machine.o | grep 'Machine::'
nm -C main.o  | grep 'Machine::'   # shows what main wants
g++ main.o machine.o user.o -o sucrose

If nm -C machine.o shows a Machine::Machine() symbol (type T or t), the definitions exist and the remaining issue is with linking: make sure the final link actually uses that machine.o (no stale .o with same name in another directory) and that g++ (not ld or gcc without C++ libs) is doing the link. If machine.o has no Machine symbols, the .cpp either didn't define the methods with the exact same qualified name or the wrong source file was compiled. Common culprits: the class/definitions are inside a namespace in the header but implemented without the namespace qualifier in the .cpp, a typo in the class name, or an accidental different signature (e.g., constructor parameters). Also check for leftover object files; run a full make clean and rebuild.

If the object contains symbols but linking still fails, enable verbose linking (g++ -v) or show the actual linker command from make to confirm the right object files are passed. These checks will quickly point to whether the issue is a missing link object or a mismatched definition.

Recommended Answers

All 5 Replies

Is machine.cpp in your project / makefile?
Do you ever see machine.cpp being compiled when main.cpp is compiled?

Doesn't get to compile at all. The error keeps it from getting that far.

Makefile

OBJS = main.o machine.o user.o
EXENAME = sucrose
C = g++
COPTS = -g -Wall
LINK = g++
LINKOPTS = -o $(EXENAME)

$(EXENAME):  $(OBJS) $(LINK) $(LINKOPTS) $(OBJS)

main.o : main.cpp machine.h
        $(C) $(COPTS) main.cpp

machine.o : machine.cpp machine.h user.h
        $(C) $(COPTS) machine.cpp

user.o : user.cpp user.h
        $(C) $(COPTS) user.cpp

clean:
        -rm *o $(EXENAME)

> $(EXENAME): $(OBJS) $(LINK) $(LINKOPTS) $(OBJS)
Shouldn't this be

$(EXENAME):  $(OBJS)
        $(LINK) $(LINKOPTS) $(OBJS)

Also, COPTS should also include the -c command line flag. This means "compile only", and is necessary to turn each file into its corresponding .o file. As written, each target tries to compile and link the whole program, which will cause the problem you see.

Does typing this at the command line also work? g++ main.cpp machine.cpp user.cpp

I made those changes and it still doesn't work. The command-line statement gave the same error too.

Thanks for waiting a whole month to post "it doesn't work" :icon_rolleyes:

Now try again and post some actual command lines, actual error messages, and the code if it's different from what you originally posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.