Compiling crafty 25.6

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Cardoso
Posts: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Compiling crafty 25.6

Post by Cardoso »

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: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Compiling crafty 25.6

Post by Ras »

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: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: Compiling crafty 25.6

Post by Cardoso »

Thanks Ras,
I added "#pragma once" in lock.h but it didn't help.
What am I missing?
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Compiling crafty 25.6

Post by Ras »

Cardoso wrote: Thu Apr 09, 2020 3:25 pmI 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: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Compiling crafty 25.6

Post by bob »

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: 362
Joined: Thu Mar 16, 2006 7:39 pm
Location: Portugal
Full name: Alvaro Cardoso

Re: Compiling crafty 25.6

Post by Cardoso »

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: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Compiling crafty 25.6

Post by bob »

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