# How/why does this work? Setup for cross-compiling and unit tests on host

**URL:** https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120
**Category:** Usage
**Tags:** os:linux
**Created:** [September 20, 2021, 11:37am UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120 "2021-09-20T11:37:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![andresovela](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andresovela/32/1808_2.png) [@andresovela](https://discourse.cmake.org/u/andresovela)
#### Post date: [September 20, 2021, 11:37am UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120/1 "2021-09-20T11:37:18Z")

</div>

Hi all, first timer here.

I am a CMake newbie and I’ve been trying to set up a project in CMake in which I can cross-compile for ARM Cortex-M targets, and compile some unit tests to run on the host machine.

In my research I stumbled upon many conflicting statements, whether CMake can’t handle multiple toolchains, whether it can, whether you need some extra shell scripts or CLI options, etc.

In the end I managed to get it to work but I have no idea why it works. The project is quite simple as it is just a template for now, you can take a look in [here](https://github.com/andresovela/cmake-arm-gcc-project-template).

First I set up my cross-compiling toolchain as default in the root CMakeLists.txt. That works fine for the cross-compilation target.  
For the host compilation what I did was just very naive and I just set the `CMAKE_XXX_COMPILER` variables in order to override the cross-compilation toolchain _after_ I have declared a the project for the unit tests. It doesn’t work if you override them _before_ the `project()` call.

This setup does work and compile the files correctly, but I don’t know if I’m somehow shooting myself in the foot.

I’d appreciate any feedback on this, suggestions to improve the setup, etc.  
Thank you!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 20, 2021, 2:18pm UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120/2 "2021-09-20T14:18:00Z")

</div>

CMake doesn’t support multiple toolchains. Changing the `CMAKE_<LANG>_COMPILER` variables other than through `project()` or `enable_language()` is deep into “nothing is guaranteed” behavior. You’ll need two projects to support host and target artifacts.

---

<div class="post-metadata">

### Author: ![andresovela](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andresovela/32/1808_2.png) [@andresovela](https://discourse.cmake.org/u/andresovela)
#### Post date: [September 20, 2021, 4:07pm UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120/3 "2021-09-20T16:07:27Z")

</div>

What do you mean with two “projects”? Two `project()` calls or two completely separate CMakeLists.txt with separate roots?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 20, 2021, 4:12pm UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120/4 "2021-09-20T16:12:45Z")

</div>

Err, sorry. I mean build directories.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [September 20, 2021, 9:42pm UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120/5 "2021-09-20T21:42:58Z")

</div>

You can use ExternalProject to build with a different toolchain to the main build. You would have to specify the toolchain details explicitly to avoid the main build’s details being used instead. Sorry if that’s a bit vague, low on time today, but hopefully that gives you a place to investigate next at least.

---

<div class="post-metadata">

### Author: ![andresovela](https://discourse.cmake.org/user_avatar/discourse.cmake.org/andresovela/32/1808_2.png) [@andresovela](https://discourse.cmake.org/u/andresovela)
#### Post date: [September 21, 2021, 7:43am UTC](https://discourse.cmake.org/t/how-why-does-this-work-setup-for-cross-compiling-and-unit-tests-on-host/4120/6 "2021-09-21T07:43:57Z")

</div>

I’ll look into it, thank you 🙂
