0

Currently I have a program that has a main.cpp, linked_queue.cpp and a linked_queue.h, and I am trying to write a simple Makefile, but I am running into a linker issue.

In main.cpp I have included the header file, #include"linked_queue.h", but I am getting a linker error. The following is there error on my machine:

clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic -c linked_queue.cpp
clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic -c main.cpp
clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic  linked_queue.o main.o -o main
Undefined symbols for architecture x86_64:
  "LinkedQueue<int>::push(int)", referenced from:
      _main in main.o
  "LinkedQueue<int>::~LinkedQueue()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is the current contents of my Makefile:

main:  linked_queue.o main.o
    clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic  linked_queue.o main.o -o main

linked_queue.o: linked_queue.cpp linked_queue.h
    clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic -c linked_queue.cpp

main.o: main.cpp linked_queue.h
    clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic -c main.cpp


clean:
    rm -rf *.o *.dSYM main

I thought I included all the dependencies. Any thoughts on what I have missed?

James
  • 170
  • 1
  • 8
  • In `clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic linked_queue.o main.o -o main`, try changing the order of the two .o files. – simon Sep 16 '22 at 18:23
  • `clang++ -g3 -std=c++1z -Wconversion -Wall -Werror -Wextra -pedantic main.o linked_queue.o -o main` made the change but still the error. I initially thought that would be the issue. – James Sep 16 '22 at 18:25
  • Have you defined the `push(int)` method inside `LinkedQueue` class? – Aamir Sep 16 '22 at 18:49
  • @Aamir, yes I have – James Sep 16 '22 at 18:51
  • 2
    Is `LinkedQueue` a template that has [been implemented in a cpp file](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file)? – user4581301 Sep 16 '22 at 18:52
  • That is exactly what is happening! I thought you could just implement templates in a `.cpp`. Appreciate this! – James Sep 16 '22 at 19:16

0 Answers0