How do I best use #include to prevent my multiple definition error? [closed]

While implementing three files (tree.h, tree.cpp, and node.h) into an existing project, I’ve run into "multiple definition of" errors when trying to reference them in my parser.h and parser.cpp files. I am using include guards to prevent multiple inclusion but I don’t think that is doing what I want it to. I am also using plain C++99 and compiling on a Linux machine, I don’t have control over that.

The getNode() is defined with the node_t struct in node.h and both need to be available to parser.cpp, parser.h, tree.cpp, and tree.h.

[user]$ make
g++ -g -Wall -Wno-unused-variable -o frontend main.o scanner.o parser.o tree.o
scanner.o: In function `getNode(std::string)':
/home/user/Compilers/P2/node.h:21: multiple definition of `getNode(std::string)'
main.o:/home/user/Compilers/P2/node.h:21: first defined here
parser.o: In function `getNode(std::string)':
/home/user/Compilers/P2/node.h:21: multiple definition of `getNode(std::string)'
main.o:/home/user/Compilers/P2/node.h:21: first defined here
tree.o: In function `getNode(std::string)':
/home/user/Compilers/P2/node.h:21: multiple definition of `getNode(std::string)'
main.o:/home/user/Compilers/P2/node.h:21: first defined here
collect2: error: ld returned 1 exit status
make: *** [frontend] Error 1

Makefile
I tried adding node.h and token.h to the OBJFILES but that only caused those files to be deleted when I did make clean

CC = g++
CFLAGS = -g -Wall -Wno-unused-variable
CXXFLAGS = -g

OBJFILES = main.o scanner.o parser.o tree.o 
TARGET = frontend

all: $(TARGET)


$(TARGET): $(OBJFILES)
    $(CC) $(CFLAGS) -o $(TARGET) $(OBJFILES)


.PHONY: clean
    
clean:
    rm -f $(OBJFILES) $(TARGET) *~

I took all my files and their #include statements and header guards and mocked up a diagram of what all is happening.
visual representation of my files

Source: Windows Questions C++

LEAVE A COMMENT