# Compile unit test natively and cross compile code for embedded target

**URL:** <https://discourse.cmake.org/t/compile-unit-test-natively-and-cross-compile-code-for-embedded-target/3607>\
**Category:** Usage\
**Created:** [June 22, 2021, 3:03pm UTC](https://discourse.cmake.org/t/compile-unit-test-natively-and-cross-compile-code-for-embedded-target/3607 "2021-06-22T15:03:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![murtiko](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@murtiko](https://discourse.cmake.org/u/murtiko)\
**Post date:** [June 22, 2021, 3:03pm UTC](https://discourse.cmake.org/t/compile-unit-test-natively-and-cross-compile-code-for-embedded-target/3607/1 "2021-06-22T15:03:26Z")

</div>

Hello,

I am working on a project for an embedded MCU (STM32). And I am trying to set up unit testing with googletest which builds and runs natively.

1. How can I build these two with one single `cmake --build` command?

I was thinking of using `ExternalProject_Add()` in the top-level CMakeLists.txt to add the `lib` folder and re-configure with the native compiler at the build time.

```auto
./project
  ./src
     main.cpp 
     CMakeLists.txt
  ./lib
     lib_a.cpp
     lib_a.hpp
     lib_test.cpp
     CMakeLists.txt
  CMakeLists.txt 

```

`lib_test.cpp `: Should build natively using GCC and run on the host.  
`main.cpp` : Should build using Arm toolchain.

1. What would be the best directory structure for this project?

Do you think `lib_test.cpp` would be better in a separate folder (i.e. `tests`)? and why?

---

<div class="post-metadata">

**Author:** ![murtiko](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@murtiko](https://discourse.cmake.org/u/murtiko)\
**Post date:** [June 23, 2021, 8:47am UTC](https://discourse.cmake.org/t/compile-unit-test-natively-and-cross-compile-code-for-embedded-target/3607/2 "2021-06-23T08:47:35Z")

</div>

It looks as though the first question has already been answered before:

> [@Using multiple toolchains in the same project?](https://discourse.cmake.org/t/using-multiple-toolchains-in-the-same-project/1505):
>
> I’m looking at adding proper support for WebAssembly to the CMake build files for a project (Halide) and am a little unclear as to whether there is a simple way to make this happen. Here’s the problem: Halide is, itself, a cross-compiler; any instance of libHalide can generate object code for any of the supported targets for which it is configured. For performance reasons, it’s extremely desirable that libHalide be compiled to the native ‘host’ configuration, so on (say) x64-Linux, we alwa…
