0

I'm trying to make an array representing a chess board, and I did it like so (according to the similar solved problems on this site)

board.h

namespace Board {
    extern int squares[];
};

board.cpp

#include "board.h"

int Board::squares[64];

Yes, it's suppose to be empty, I'll later fill it with values, but the problem is in my render function that's drawing the sprites, I have to access them to know what to draw

game.cpp

int piece = Board::squares[index];

That did not throw any error when compiling, but when linking it throws... this

/usr/bin/ld: CMakeFiles/cpp-chess.dir/src/game.cpp.o: warning: relocation against `_ZN5Board7squaresE' in read-only section `.text'
/usr/bin/ld: CMakeFiles/cpp-chess.dir/src/game.cpp.o: in function `Game::RenderPiece()':
game.cpp:(.text+0x9d3): undefined reference to `Board::squares'

What can I do? Thanks a lot in advance!

toara
  • 1
  • 2
    Check that you are compiling and _linking_ board.cpp. Not doing that is the standard issue with this common question. – Avi Berger Sep 02 '22 at 00:28
  • Re: “it's supposed to be empty” — it’s not empty. It’s initialized with zeros. Global and namespace scoped objects of built in types are initialized with zeros. – Pete Becker Sep 02 '22 at 00:44
  • Thanks for pointing out it's not empty, I guess I touched too much JavaScript stuff – toara Sep 02 '22 at 01:21

1 Answers1

0

Thanks a lot to Avi Berger, I realized that I've totally butchered the whole compiling and linking process, oops. It turns out that it all came down to me forgetting to rerun cmake after I added those files, silly me. I appreciate the help a lot, again, thanks everyone!

toara
  • 1