Tinkering with stockfish code: C++ question

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Tinkering with stockfish code: C++ question

Post by maksimKorzh »

Hi guys

I've forked Stockfish's code and tinkering with the code.
My ultimate goal is to make xiangqi fork of stockfish.
I'm comfortable with C but new to C++.

Here's the stuff that confuses me:

I'm adding new data field to Position class:

Code: Select all

class Position {
public:
  ...

private:
  // Data members
  Piece board[SQUARE_NB];
  Bitboard byTypeBB[PIECE_TYPE_NB];
  Bitboard byColorBB[COLOR_NB];
  int pieceCount[PIECE_NB];
  Square pieceList[PIECE_NB][16];
  int index[SQUARE_NB];
  int castlingRightsMask[SQUARE_NB];
  Square castlingRookSquare[CASTLING_RIGHT_NB];
  Bitboard castlingPath[CASTLING_RIGHT_NB];
  int gamePly;
  Color sideToMove;
  Score psq;
  Thread* thisThread;
  StateInfo* st;
  bool chess960;
  
  int newField;
This gives me a warnings when compile:

Code: Select all

position.h:72:7: warning: type ‘struct Position’ violates the C++ One Definition Rule [-Wodr]
...
position.h:183:7: note: the first difference of corresponding definitions is field ‘newField’
   int newField;
I've already read about one definition rule but I don't understand how is it possible that
compiler thinks I'm having more than one definition of Position class.

How can I add new field to Position class without getting this warning?
Thanks in advance.
Joost Buijs
Posts: 1568
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Tinkering with stockfish code: C++ question

Post by Joost Buijs »

This is pretty strange indeed. It looks as if there is a second definition for the Position class somewhere else in the code.
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Tinkering with stockfish code: C++ question

Post by maksimKorzh »

Joost Buijs wrote: Sun Mar 14, 2021 11:59 am This is pretty strange indeed. It looks as if there is a second definition for the Position class somewhere else in the code.
Hi Joost, exactly, I thought so as well...
I mean just to fork SF and add this single line: int newField; and it gives this error.
Where on earth Positiocn class can be defined else where?? Any ideas?
Joost Buijs
Posts: 1568
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Tinkering with stockfish code: C++ question

Post by Joost Buijs »

maksimKorzh wrote: Sun Mar 14, 2021 12:03 pm
Joost Buijs wrote: Sun Mar 14, 2021 11:59 am This is pretty strange indeed. It looks as if there is a second definition for the Position class somewhere else in the code.
Hi Joost, exactly, I thought so as well...
I mean just to fork SF and add this single line: int newField; and it gives this error.
Where on earth Positiocn class can be defined else where?? Any ideas?
No, I haven't been looking at Stockfish code for ages, but I can download it and take a look, is it the latest dev. version?
Ras
Posts: 2498
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Tinkering with stockfish code: C++ question

Post by Ras »

maksimKorzh wrote: Sun Mar 14, 2021 11:48 amI've already read about one definition rule but I don't understand how is it possible that compiler thinks I'm having more than one definition of Position class.
Did you try "make clean" to remove object files compiled against the previous definition of Position?
Rasmus Althoff
https://www.ct800.net
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Tinkering with stockfish code: C++ question

Post by maksimKorzh »

Joost Buijs wrote: Sun Mar 14, 2021 12:06 pm
maksimKorzh wrote: Sun Mar 14, 2021 12:03 pm
Joost Buijs wrote: Sun Mar 14, 2021 11:59 am This is pretty strange indeed. It looks as if there is a second definition for the Position class somewhere else in the code.
Hi Joost, exactly, I thought so as well...
I mean just to fork SF and add this single line: int newField; and it gives this error.
Where on earth Positiocn class can be defined else where?? Any ideas?
No, I haven't been looking at Stockfish code for ages, but I can download it and take a look, is it the latest dev. version?
yeah, just forked via git clone yesterday, I would appreciate a lot, thank you!
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Tinkering with stockfish code: C++ question

Post by maksimKorzh »

Ras wrote: Sun Mar 14, 2021 12:08 pm
maksimKorzh wrote: Sun Mar 14, 2021 11:48 amI've already read about one definition rule but I don't understand how is it possible that compiler thinks I'm having more than one definition of Position class.
Did you try "make clean" to remove object files compiled against the previous definition of Position?
OMG! What a noob I am!
This worked!
Thank you so much, Ras!
I just totally forgot that the difference in definitions is arising from previously compiled object files during linking.
Ahh... how could I not guess that on my own!

Thank you thousand times!
User avatar
maksimKorzh
Posts: 771
Joined: Sat Sep 08, 2018 5:37 pm
Location: Ukraine
Full name: Maksim Korzh

Re: Tinkering with stockfish code: C++ question

Post by maksimKorzh »

Joost Buijs wrote: Sun Mar 14, 2021 12:06 pm
maksimKorzh wrote: Sun Mar 14, 2021 12:03 pm
Joost Buijs wrote: Sun Mar 14, 2021 11:59 am This is pretty strange indeed. It looks as if there is a second definition for the Position class somewhere else in the code.
Hi Joost, exactly, I thought so as well...
I mean just to fork SF and add this single line: int newField; and it gives this error.
Where on earth Positiocn class can be defined else where?? Any ideas?
No, I haven't been looking at Stockfish code for ages, but I can download it and take a look, is it the latest dev. version?
Sorry for bothering, I just forgot to "make clean" after changed Position class... ))))
Joost Buijs
Posts: 1568
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: Tinkering with stockfish code: C++ question

Post by Joost Buijs »

It was a link error, no problem.
User avatar
hgm
Posts: 27860
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Tinkering with stockfish code: C++ question

Post by hgm »

Ras wrote: Sun Mar 14, 2021 12:08 pm
maksimKorzh wrote: Sun Mar 14, 2021 11:48 amI've already read about one definition rule but I don't understand how is it possible that compiler thinks I'm having more than one definition of Position class.
Did you try "make clean" to remove object files compiled against the previous definition of Position?
Sounds like a broken Makefile, right?