Cross compiling with Clang

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
User avatar
lucasart
Posts: 2957
Joined: Mon May 31, 2010 11:29 am
Contact:

Cross compiling with Clang

Post by lucasart » Sun Nov 12, 2017 12:28 am

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: 69
Joined: Thu Oct 06, 2016 7:17 pm
Location: India
Contact:

Re: Cross compiling with Clang

Post by mkchan » Sun Nov 12, 2017 5:11 am

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: 672
Joined: Tue May 22, 2007 9:13 am

Re: Cross compiling with Clang

Post by Rein Halbersma » Sun Nov 12, 2017 6:56 am

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: 2957
Joined: Mon May 31, 2010 11:29 am
Contact:

Re: Cross compiling with Clang

Post by lucasart » Sun Nov 12, 2017 8:52 am

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: 98
Joined: Thu Jul 24, 2014 12:49 pm

Re: Cross compiling with Clang

Post by pferd » Sun Nov 12, 2017 10:31 am

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: 951
Joined: Tue Aug 30, 2016 6:19 pm
Contact:

Re: Cross compiling with Clang

Post by Ras » Sun Nov 12, 2017 10:33 am

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

Post Reply