Compile unit test natively and cross compile code for embedded target

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.

./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?

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