Linker trouble with g++

Hey there,
I've been working on a small Terminal-Snake Program to better understand C++.
This project was started off with just a crude outline of what it would become and after a while I realised that it needed more structure so I went ahead and divided the code into a few different files while trying to keep it as clean as possible.
At this point I started getting these compilation errors where the linker apparently fails to link some stuff (This is how it's looking like currently, but the errors were all pretty similar):

make -k
rm TSnake.x || true
g++ Consumables.cpp Snake.cpp TSnake.cpp -o TSnake.x -l ncurses -g
/sbin/ld: /tmp/ccUig2o6.o: in function `mangeInput()':
/home/maus/Cxx/TSnake/TSnake.cpp:92: undefined reference to `useConsumable(int, int)'
/sbin/ld: /tmp/ccUig2o6.o: in function `main':
/home/maus/Cxx/TSnake/TSnake.cpp:120: undefined reference to `insertConsumable(ConsumableType, int, int, int, int)'
/sbin/ld: /home/maus/Cxx/TSnake/TSnake.cpp:122: undefined reference to `tickGameObjects()'
/sbin/ld: /home/maus/Cxx/TSnake/TSnake.cpp:128: undefined reference to `insertConsumable(ConsumableType, int, int, int, int)'
/sbin/ld: /home/maus/Cxx/TSnake/TSnake.cpp:133: undefined reference to `insertConsumable(ConsumableType, int, int, int, int)'
collect2: error: ld returned 1 exit status
make: *** [makefile:9: all] Error 1

I figured that this might be related to circular dependencies so I changed my project structure to this:

Consumables.cpp < Consumables.h > TSnake.cpp
Snake.cpp < Snake.h > TSnake.cpp

This sadly turned out to be unsuccessful so my best guess is that this is related to my g++ flags. I tried searching for problems like this on the web, but including Consumables.cpp and Snake.cpp into the list of to-compile files is all that is being suggested on the web.
I have no clue what else I could do so I decided to seek help in this very 90s but adequate forum ;)
This is the repository of my code, if anyone is interested: https://gitlab.com/UltraBlackLinux/tsnake
Thanks!
(--I sure hope that the formatting worked out as I intended. The Preview button does not appear to be showing anything apart from a box with my Username.-- Formatting did not work.)
Last edited on
undefined reference to `useConsumable(int, int)'
Check where your declare useConsumble (presumably in a .h file), and define it (presumably in a .cpp file). Does the signature of the declaration match with the definition, and do these match with the way it is actually being called? All three of these need to match.

You call it as useConsumable(newSegment.x, newSegment.y); but you define it as
1
2
void useConsumable(int x, int y, int *snakeSize, void (*enableBoost)(),
                   void *reverseSnake());
Last edited on
ll three of these need to match.

Oh thank you I didn't even notice that I messed that up. (Emacs clangd stuff seems to not be working as I would like it to)
I apparently added new functionalities to some stuff in the game and forgot to modify the headers. I guess that won't ever happen to me again xD
Thank you so much!
Topic archived. No new replies allowed.