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

Re: Tinkering with stockfish code: C++ question

Post by maksimKorzh »

hgm wrote: Sun Mar 14, 2021 12:40 pm
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?
Probably makefile does contain a proper target for debug even though I don't know whether it is so or not and how to use it.
The way I build is: make build ARCH=x86-64 COMP=gcc

and the last line before it compiles: g++ -o stockfish benchmark.o bitbase.o bitboard.o endgame.o evaluate.o main.o material.o misc.o movegen.o movepick.o pawns.o position.o psqt.o search.o thread.o timeman.o tt.o uci.o ucioption.o syzygy/tbprobe.o -m64 -Wl,--no-as-needed -lpthread -Wall -Wcast-qual -fno-exceptions -std=c++11 -pedantic -Wextra -Wshadow -m64 -DNDEBUG -O3 -DIS_64BIT -msse -flto

feels like it's a build for release, not debug, so I guess the problem is that I don't know how to properly work with multiple files in C/C++.
I only mostly did single file engines hence never was bothered by thinking about the linking process.

However the object files holding Position class should be recompiled if change occurs but this didn't happen hence this error, so the
linker thought as if there were 2 different Position class definitions.

Btw, I had a very surprising experiment today - my JS engine passes perft 5 in starting positions almost as fast as Fairy SF and even faster than Chameleon (SF 7 derivative that plays xiangqi). Here's a video:


Fairy SF author Fabien Fitcher explained me the reasons behind it here:
https://github.com/ianfab/Fairy-Stockfish/issues/278
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Tinkering with stockfish code: C++ question

Post by Ras »

hgm wrote: Sun Mar 14, 2021 12:40 pmSounds like a broken Makefile, right?
The dependency on header files is missing, that's why a header file change doesn't lead to recompilation.
Rasmus Althoff
https://www.ct800.net
User avatar
hgm
Posts: 27795
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 »

Exactly. A proper Makefile would list all dependencies, so that if change in one file would require recompilation of any other, that recompilation would be done even if the object file already existed.

If you have to do "make clean" first (which throws away all object files, so they will all have to be recompiled always), you might as well have given the command gcc -c *.c, and the Makefile is absolutely useless.

And yes, Fairy-Stockfish is rather slow. It uses bitboard. So no surprise there.
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Tinkering with stockfish code: C++ question

Post by Ras »

hgm wrote: Sun Mar 14, 2021 1:39 pmIf you have to do "make clean" first (which throws away all object files, so they will all have to be recompiled always)
That's not the intended, but the actual way to do it. I've been bitten often enough by such Makefiles that I don't waste my time hunting down obscure bugs. For small projects, the recompilation time is not too bad, and for complex projects, you can safely bet that some dependencies will have been forgotten to be modelled. That's even without considering that Make is fundamentally broken anyway because it doesn't take Make options into account for deciding whether to recompile, and using the file system time for an "is newer" comparison is brittle.
you might as well have given the command gcc -c *.c
Try that with Crafty's #includes of .c files from an era without LTO.
and the Makefile is absolutely useless.
What it does give you is shell independence - at the expense of having to open a terminal at all and, for multi-architecture builds, typing in the target like in the Stockfish Makefile. Which is why I'm using buildscripts instead.
Rasmus Althoff
https://www.ct800.net
BeyondCritics
Posts: 396
Joined: Sat May 05, 2012 2:48 pm
Full name: Oliver Roese

Re: Tinkering with stockfish code: C++ question

Post by BeyondCritics »

Actually the dependencies might be ok, they are generated automagically by the compiler and then put into hidden .d files.
I think the real problem is, that make makes it very uncomfy to work with several different binary versions. Hence the stockfish make file simply bangs out all object files into the source directory. And of course, since make does not understag flags, this stale file can get into your way later.
My tip: Install ccache (https://ccache.dev/). I believe it is an absolut must have for every stockfish hacker. Under Ubuntu i simply say

Code: Select all

sudo apt install -y ccache
and i am done. Also, i recommend rlwrap, to work comfortably with stockfish from the comannd line.
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 »

BeyondCritics wrote: Sun Mar 14, 2021 2:32 pm Actually the dependencies might be ok, they are generated automagically by the compiler and then put into hidden .d files.
I think the real problem is, that make makes it very uncomfy to work with several different binary versions. Hence the stockfish make file simply bangs out all object files into the source directory. And of course, since make does not understag flags, this stale file can get into your way later.
My tip: Install ccache (https://ccache.dev/). I believe it is an absolut must have for every stockfish hacker. Under Ubuntu i simply say

Code: Select all

sudo apt install -y ccache
and i am done. Also, i recommend rlwrap, to work comfortably with stockfish from the comannd line.
thanks for a tip