My project’s target is an embedded MCU. I cross-compile on my host.
I want to write unit tests, and include them in my cmake project structure. The unit tests are for testing module logic, so not running on the target but on the host.
Here is an illustrating structure:
| application.c
| CMakeLists.txt
+ utils/
| filter.c
| filter.h
+ tests
| test_filter.c
| CMakeLists.txt
I can build an embedded application that uses application.c
, and filter.[ch]
.
From what I have gathered online, this is my attempt to include the building of the tests for the host architecture:
- added this to the root’s
CMakeLists.txt
:
enable_testing()
add_subdirectory(utils/test)
- added this to the
tests/CMakeLists.txt
:
add_executable(test_filter test_filter.c)
project(filter_test C)
set(CMAKE_C_COMPILER "gcc")
add_compile_options(
"$<$<COMPILE_LANGUAGE:C>:-O0;-DNDEBUG;-std=c99;-Wall;-Werror>"
)
add_test(NAME test_filter
COMMAND test_filter)
My main issue so far, is that the flags of the embedded project follow for the compilation of the test program.
The compilation flags for my cross-compiling (arm-none-eabi-gcc
, e.g. -mthumb
, -mcpu
) don’t make sense for my host’s compiler (regular gcc
). It also tries to build in libraries irrelevant to the tests:
make[2]: Leaving directory '/projroot/build'
[ 2%] Performing build step for 'ELF2UF2Build'
[ 2%] Building C object utils/test/CMakeFiles/test_filter.dir/test_filter.c.obj
[ 3%] Performing build step for 'PioasmBuild'
[ 4%] Built target bs2_default
gcc: warning: '-mcpu=' is deprecated; use '-mtune=' or '-march=' instead
make[3]: Entering directory '/projroot/build/elf2uf2'
make[2]: Entering directory '/projroot/build'
make[3]: Entering directory '/projroot/build/pioasm'
[ 11%] Built target freertos
gcc: error: unrecognized command-line option '-mthumb'
How do I setup my tests
directory and the relevant CMakeLists.txt
, for cmake to build the tests for my host architecture, rather than the embedded MCU?