Clion Cross Compile

 

CMake uses a toolchain of utilities to compile, link libraries and create archives, and other tasks to drive the build. The toolchain utilities available are determined by the languages enabled. In normal builds, CMake automatically determines the toolchain for host builds based on.

  1. My goal is to cross-compile an application using CLion for use on an older target system. When I create my CMakeLists.txt file, I specify the cross-compiler. However, the CLion build fails in the linking process. If I run the same build from the command line, the link succeeds.
  2. Yes, you need the boost libraries which are not header only built for ARM. This SO question covers that: cross compile Boost 1.57.0 on ubuntu for arm To make things like findpackage work for cross compilation you should set CMAKEFINDROOTPATH. Suppose you set CMAKEFINDROOTPATH to /opt.
How-To'sTips & Tricks

With CMake 3.15 it has become possible to use the Clang compiler on Windows with a GNU-style command line. This also means that it’s possible to use Clang with the Mingw-w64 toolchain.

While it’s possible to configure Clang with the Mingw-w64 (or MinGW) toolchain, if you decide to use Clang from the LLVM repo, it won’t work correctly. This is because LLVM Clang for Windows is built using Microsoft Visual Studio, and all the built-in macros and include search paths are set up for use with Visual Studio. So even when used with the MinGW toolchain, it will still try to include MSVC standard library headers.

We’ve done some experiments and found a possible workflow that involves using CLion in combination with the MinGW toolchain and Clang compiler.

Clion Cross Compile

Here are our findings:

  1. Install MSYS2 from https://www.msys2.org, follow their installation guide.
  2. Get the necessary packages with the pacman tool. We installed the following (pacman -S):
    • mingw-w64-x86_64-gcc
    • mingw-w64-x86_64-clang
    • mingw-w64-x86_64-lld
    • mingw-w64-x86_64-gdb
    • mingw-w64-x86_64-make
    • mingw-w64-x86_64-polly
    • mingw-w64-x86_64-compiler-rt

This clang compiler is built with mingw-w64 and has paths and macros that correspond to this toolchain.

Now we are ready to set up the CLion toolchain. Go to Settings/Preferences Build, Execution, Deployment Toolchains:

With your new toolchain configured, you can start building your project. You can use either the default ld linker or set the lld with -DCMAKE_LINKER=lld.

Fl studio 11.1.1 reg key

Using advanced tools provided by the Clang compiler

In theory, all Clang tools should also work without any issues. However, problems can arise involving compiler-rt. compiler-rt is a set of runtime libraries which are required to use sanitizers and profiling in Clang, the current compiler_rt package was built with MinGW. But Clang requires a compiler that is built with Clang and lld.

In our case, we wanted to use profile-guided optimization. One of the options for making this work is to get the compiler-rt source code of exactly the same version as the Clang version in MSYS2. This can be challenging, so the other solution is to clone the LLVM monorepo and build the tools you need.

For -fprofile-instr-generate it might be enough to build only compiler-rt and llvm-profdata to merge the profiler results. However, to use all the tools reliably, it’s better to build Clang and lld, as well.

Clion help

Luckily we already have the required setup for that build.

Clion Cross Compile Windows

The last step is to replace the binaries in <msys2_path>mingw64libclang<clang_version>libwindows with libraries from <compiler-rt_path>cmake-build-release-mingw_clanglibwindows or from <llvm_build_path>libclang<clang_version>libwindows in case of monorepo.

Using Clang for profiling

With the compiler-rt libraries in place, it’s now possible to use the same toolchain we set up with -fprofile-instr-generate/-fprofile-instr-use flags. Let’s build LLVM for this experiment, since we already have the source code. We’ll also use -DLLVM_ENABLE_LTO=Thin for even more optimization. Go to Settings/Preferences Build, Execution, Deployment CMake:

With this CMake configuration, you can build the Clang compiler and use it, for example, to build your own project. This will generate the relevant profiler information, which should later be merged with the tool we built earlier – llvm-profdata. With the merged profile_merged.profdata file, you can finally build the optimized version of the Clang compiler:

Using custom Clang and lld

Getting the gcc-style -fprofile-generate/-fprofile-use flags to work correctly requires changing the Clang path and setting -DCMAKE_LINKER to the newly built lld. You also need a few additional tricks for LLVM : -femulated-tls and linking pthread.

Then you should repeat all the steps you performed with -fprofile-instr-generate/-fprofile-instr-use.

Clion Cross Compile Windows Linux

Conclusion

Using Clang on Windows is now possible and does not require you to have Microsoft Visual Studio installed!

We hope that in the near future, using advanced clang tools will become easier and will no longer require the manual build. Let us know if you discover any other ways to achieve the same results!

Clion Arm

Your CLion team
JetBrains
The Drive to Develop