CLion and CMake Toolchain Files

Completed

A common use case is to create a CMake toolchain file for each target system (various architectures, for examples) your application software runs on. For example, you may need to build your application for an x86_64, armv5, and armv7 targets. Thus, you'd have a CMake toolchain file to cover each.

CLion has configuration options for toolchains, but it appears completely independent - there does not appear to be a way to have it use existing CMake toolchain files. Having it use existing CMake toolchain files is attractive as both the IDE and a build server, for example, could utilize the same configuration when cross-compiling.

Is there a way to make CLion utilize existing CMake toolchain files instead of having to create duplicate "Toolchains" in the "Build, Execution, Deployment"->"Toolchains" menu?

7 comments
Comment actions Permalink

Not sure if this is what you are looking for but this is what I'm doing.  I didn't have an existing CMake tool chain file as my existing project was Makefile based.  So I created a new toolchain file.  Then in Build, Execution, Deployment/CMake I created a new profile in this case ARM-imx6  and then in CMake options:

-DCMAKE_TOOLCHAIN_FILES=/pathto/toolchain-arm.cmake -DDOARM=1   The DOARM=1 is so the source code can conditionally build for either ARM or x86 architectures.

It took me some time to workout the interactions of the various profiles, and build configurations but once that was resolved this is working fine for me and I've used it for an RPi build profile as well as for some custom hardware with a different ARM processor and tool chain. 

I also set up a GDB Remote Debug Run/Debug configuration and that works great for remote debugging using CLion.

Without seeing what might be different in your existing CMake tool chain file I don't know if there's anything that might prevent your existing file from being plug and play.

So in my current project I have the following profiles:

Debug

Release

ARM-imx6-debug

and 

ARM-imx6

The first two are for x86 builds on the host desktop.

The last two are for building for the ARM target.

Hopefully this helps.

 

0
Comment actions Permalink

Yes, it's possible to pass your toolchain file via -DCMAKE_TOOLCHAIN_FILE=... in File | Settings | Build, Execution, Deployment | CMake > CMake options

0
Comment actions Permalink

For anyone else that stumbles across this... if you want to use a toolchain file like Anna suggests, then you most likely need to ensure that the "Default" toolchain in ( File | Settings | Build, Execution, Deployment | Toolchains ) is being used. If not, then Clion tries to do too much and can override things in your toolchain file that you specify in ( File | Settings | Build, Execution, Deployment | CMake > CMake options ).

1
Comment actions Permalink

Being completely new to CLion, I’m trying to understand how to make it fit my way of working. I’m using the Hunter packet manager to fetch and build dependencies. When building dependencies, I should specify a CMake toolchain file via -DCMAKE_TOOLCHAIN_FILE, so that Hunter can build with the same tool chain that is being used by CLion. It would be really nice if CLion could either export a CMake toolchain file based on its configuration or, if CLion could honour an existing tool chain file and set configuration based on that file. 

1
Comment actions Permalink

@Ove Svensson, toolchains for CMake projects in CLion can be specified either in Settings / Preferences | Build, Execution, Deployment | Toolchains, or using -DCMAKE_TOOLCHAIN_FILE (which can be specified in the CMake options field of CMake profile). There is no possibility to export a toolchain specified in Settings / Preferences | Build, Execution, Deployment | Toolchains to a CMake toolchain file.

0
Comment actions Permalink

Was about to give up but Matt's solution worked for me after hours of frustration. Anna Falevskaya +1 for better cross-compilation toolchain support

0
Comment actions Permalink

We should use `CMAKE_TOOLCHAIN_FILE` instead of `CMAKE_TOOLCHAIN_FILES`.

 

For example, for android ndk arm64-v8a platform, we need these:

 

-DCMAKE_TOOLCHAIN_FILE=/home/zz/soft/android-ndk-r21b/build/cmake/android.toolchain.cmake -DANDROID_LD=lld -DANDROID_ABI="arm64-v8a" -DANDROID_PLATFORM=android-24

0

Please sign in to leave a comment.