Cross compiling with Clang

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Cross compiling with Clang

Post by lucasart »

Has anyone managed to cross-compile using Clang ? For Windows, and possible Android or Mac, on a Linux host, I mean.

I had a look at the Clang manual, which tells you how cross compiling is so easy with the "-target" option, and just works out of the box with Clang. Of course, they don't bother giving you actual working examples. And anything I found on internet didn't work :(

Thanks.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
mkchan
Posts: 88
Joined: Thu Oct 06, 2016 9:17 pm
Location: India

Re: Cross compiling with Clang

Post by mkchan »

Whenever I tried cross-compiling with clang I always get errors about missing the MSVC libraries so I never even got past "<iostream> header not found".
for reference i was using the following target:

Code: Select all

-target x86_64-pc-win32-elf
I just compile with the x86_64/i686-mingw-w64 set of tools instead. Works pretty ok.
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: Cross compiling with Clang

Post by Rein Halbersma »

lucasart wrote:Has anyone managed to cross-compile using Clang ? For Windows, and possible Android or Mac, on a Linux host, I mean.

I had a look at the Clang manual, which tells you how cross compiling is so easy with the "-target" option, and just works out of the box with Clang. Of course, they don't bother giving you actual working examples. And anything I found on internet didn't work :(

Thanks.
Maybe not exactly what you are looking for, but Microsoft provides images of Windows 10 with 60 day licenses with Visual Sudio pre-installed. You can run this in a virtual machine and compile your code.

https://developer.microsoft.com/en-us/w ... l-machines
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Cross compiling with Clang

Post by lucasart »

Rein Halbersma wrote:
lucasart wrote:Has anyone managed to cross-compile using Clang ? For Windows, and possible Android or Mac, on a Linux host, I mean.

I had a look at the Clang manual, which tells you how cross compiling is so easy with the "-target" option, and just works out of the box with Clang. Of course, they don't bother giving you actual working examples. And anything I found on internet didn't work :(

Thanks.
Maybe not exactly what you are looking for, but Microsoft provides images of Windows 10 with 60 day licenses with Visual Sudio pre-installed. You can run this in a virtual machine and compile your code.

https://developer.microsoft.com/en-us/w ... l-machines
Thanks. But... No, thanks.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
pferd
Posts: 134
Joined: Thu Jul 24, 2014 2:49 pm

Re: Cross compiling with Clang

Post by pferd »

Using clang for cross-compiling works ok for me.

For stockfish you have to install the mingw libs. Then add -target x86_64-w64-mingw32 to both the compiler and linker flags. Now you can compile with make build ARCH=x86-64-bmi2 COMP=clang.

Unfortunately both lto and pgo won't work for me.
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Cross compiling with Clang

Post by Ras »

lucasart wrote:Has anyone managed to cross-compile using Clang ? For Windows, and possible Android or Mac, on a Linux host, I mean.
I cross-compiled for Android, though on a Windows host. However, it should be pretty much the same on a Linux host, modulo some path issues.

Download Android NDK (also for Linux):
https://developer.android.com/ndk/downloads/index.html

What I did:
http://www.talkchess.com/forum/viewtopi ... 33&t=64553

And here the build script I'm using (actually, I'm using the batch version for Windows). You'll only have to change compiler_path. The script assumes that the C files are in the same path as the script itself, and that there is an "output" directory in this path where the executable will be put to.

Code: Select all

#!/bin/bash
# currently, Android NDK r15b has been used with the standalone toolchain generated with
# make_standalone_toolchain.py --arch arm --api 16 --unified-headers --install-dir c&#58;\android-standalone-chain
# *** edit this to point to your Android Clang.
compiler_path="C&#58;/android-standalone-chain/bin"
# *** derived variables - do not edit
compiler="$compiler_path/clang"
strip="$compiler_path/arm-linux-androideabi-strip"
# save current directory
starting_dir=$&#40;pwd&#41;
# change the path to where this script is - useful in case this script
# is being called from somewhere else, like from within an IDE
cd "$( cd "$( dirname "$&#123;BASH_SOURCE&#91;0&#93;&#125;" )" && pwd )"
# *** the source files are fetched relative to the path of this script
compiler_options="-DTARGET_BUILD32 -march=armv7-a -m32 -mfloat-abi=softfp -mfpu=vfpv3-d16 -pie -fPIE -Wl,--fix-cortex-a8 -Weverything -Wuninitialized -Wstrict-aliasing -Wno-unused-command-line-argument -O2 -std=c99 -fno-strict-aliasing -fno-strict-overflow -static"
"$compiler" $compiler_options -o output/ct800_v1.12_and play.c move_gen.c book.c hashtables.c eval.c search.c util.c kpk.c
"$strip" --strip-unneeded output/ct800_v1.12_and
# go back to the starting directory
cd "$starting_dir"
read -n1 -r -p "press any key to continue..." key