Compiling crafty 25.6

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
Cardoso
Posts: 301
Joined: Thu Mar 16, 2006 6:39 pm
Location: Portugal
Full name: Alvaro Cardoso
Contact:

Compiling crafty 25.6

Post by Cardoso » Thu Apr 09, 2020 1:07 pm

Hi,
I'm trying to compile crafty 25.6 using VS2019.
I removed the includes in crafty.c and added all .h and .c files except probe.c, tb*.c and stdendian.h
I gives a linker error:

Code: Select all

>analyze.obj : error LNK2005: Pause already defined in analyze.obj
anyone has an idea what is going on?

thanks,
Alvaro

Ras
Posts: 1381
Joined: Tue Aug 30, 2016 6:19 pm
Full name: Rasmus Althoff
Contact:

Re: Compiling crafty 25.6

Post by Ras » Thu Apr 09, 2020 1:19 pm

lock.h establishes the function Pause() as global symbol in line 33. Originally, that wasn't a problem because evverything was included into one C file so that this function appeared only once. And that was because there's probably some header guards that guard against multiple inclusion in the same file. When you include C files, you have several files on disk, but it's only one compilation unit.

But now you have several compilation units, so this include guards now works per file, and that's why the Pause() function is established in several files.

Actually, the whole way how Crafty organises the code is not favoured anymore these days. You generally don't include C files that have actual code, and you don't define functions in header files. However, back when Crafty was created, there was no link time optimisation yet, so that was the only way to make the compiler see and optimise all code at once, which gave a de facto link time optimisation even without compiler support. Certainly hacky even back then, but justified when squeezing the last drop of performance.
Rasmus Althoff
https://www.ct800.net

Cardoso
Posts: 301
Joined: Thu Mar 16, 2006 6:39 pm
Location: Portugal
Full name: Alvaro Cardoso
Contact:

Re: Compiling crafty 25.6

Post by Cardoso » Thu Apr 09, 2020 1:25 pm

Thanks Ras,
I added "#pragma once" in lock.h but it didn't help.
What am I missing?

Ras
Posts: 1381
Joined: Tue Aug 30, 2016 6:19 pm
Full name: Rasmus Althoff
Contact:

Re: Compiling crafty 25.6

Post by Ras » Thu Apr 09, 2020 1:32 pm

Cardoso wrote:
Thu Apr 09, 2020 1:25 pm
I added "#pragma once" in lock.h but it didn't help.
What am I missing?
You're compiling the C files as individual translation units. When one file includes lock.h, your pragma guards against a second inclusion in that same C file, but not against an inclusion in another C file. You can compile all C files, but when linking, the linker finds the same function name in two different object files.

Before, since all the C file were included directly, the actual compiler run would only see a single huge C file, i.e. a single translation unit.
Rasmus Althoff
https://www.ct800.net

bob
Posts: 20896
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Compiling crafty 25.6

Post by bob » Thu Apr 09, 2020 4:46 pm

There is still a "crafty.c" file that #includes all source files. That should avoid this at the expense of increased compile time with the gain of better function inlining/unrolling.

Cardoso
Posts: 301
Joined: Thu Mar 16, 2006 6:39 pm
Location: Portugal
Full name: Alvaro Cardoso
Contact:

Re: Compiling crafty 25.6

Post by Cardoso » Thu Apr 09, 2020 5:40 pm

Thanks,
I tried this

Code: Select all

__forceinline void Pause() {
}
And it compiled ok.
Bob is there any problem using __forceinline with Pause()?

bob
Posts: 20896
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

Re: Compiling crafty 25.6

Post by bob » Thu Apr 09, 2020 7:58 pm

Should inline naturally, but it won't hurt. Or you can just compile "crafty.c" which will solve it as well, maybe faster executable.

Post Reply