-1

I run a Cortex-M Project (ARM). Here, I would like to use the CMake feature add_test where I then can go ahead and call make test.

This works just fine.

The issue I'm having is that the unit test I've added shall run on my PC (x86) not on ARM, therefore, the test fails.

My subproject is:

project(dummy_math_lib_2)

add_library(${PROJECT_NAME}
        src/dummy_math_lib_2.cpp )
        
add_test(${PROJECT_NAME}
    src/dummy_math_lib_2.cpp
    unittest/src/unittest.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC inc)

target_link_libraries(${PROJECT_NAME})

In the main CMakeLists.txt file, I enable "ENABLE_TESTING()" then I do the following:

cmake ..
make test

The sub project tree looks as follows:

.
├── CMakeLists.txt
├── inc
│   └── dummy_math_lib_2.hpp
├── src
│   └── dummy_math_lib_2.cpp
└── unittest
    ├── inc
    └── src
        └── unittest.cpp

EDIT:

project(dummy_math_lib_2)

add_library(${PROJECT_NAME}
        src/dummy_math_lib_2.cpp )
        
target_include_directories(${PROJECT_NAME} PUBLIC inc)

add_executable(UnitTest1 
    unittest/src/test.cpp
)

add_test(Test1 UnitTest1)

output:

-- Configuring done
-- Generating done
-- Build files have been written to: /home/linux/workspace/cmake_t000D_cmake_DK/BUILD
Running tests...
Test project /home/linux/workspace/cmake_t000D_cmake_DK/BUILD
    Start 1: Test1
Could not find executable UnitTest1
Looked in the following places:
UnitTest1
UnitTest1
Release/UnitTest1
Release/UnitTest1
Debug/UnitTest1
Debug/UnitTest1
MinSizeRel/UnitTest1
MinSizeRel/UnitTest1
RelWithDebInfo/UnitTest1
RelWithDebInfo/UnitTest1
Deployment/UnitTest1
Deployment/UnitTest1
Development/UnitTest1
Development/UnitTest1
Unable to find executable: UnitTest1
1/1 Test #1: Test1 ............................***Not Run   0.00 sec

0% tests passed, 1 tests failed out of 1

Total Test time (real) =   0.00 sec

The following tests FAILED:
      1 - Test1 (Not Run)
Errors while running CTest
make: *** [Makefile:104: test] Error 8

EDIT2

Ok i found the issue. The solution is the following:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..
make # <--- FIRST BUILD IT!
make test <--- THEN RUN THE TEST

This in theory ;-) Now I have to figure out how I can build this for x86 instead of ARM. But so far ok, thanks everyone helping me out.

Kevin
  • 16,549
  • 8
  • 60
  • 74
user654789384
  • 305
  • 1
  • 4
  • 21
  • So? Do you have a question to ask? So how do you want to solve this problem? – KamilCuk Aug 10 '20 at 14:00
  • Try using the `CMAKE_CROSSCOMPILING_EMULATOR` variable in your toolchain file (https://cmake.org/cmake/help/latest/variable/CMAKE_CROSSCOMPILING_EMULATOR.html). I haven't used it myself, so hopefully the docs provide enough information for you. – Stephen Newell Aug 10 '20 at 14:37

1 Answers1

1

The add_test() command should not typically contain source files. It should simply call the executable, with any necessary command line arguments.

You first need to use add_executable() to specify the actual unit test executable, and you can reference this target in add_test:

add_library(${PROJECT_NAME}
    src/dummy_math_lib_2.cpp
)
        
target_include_directories(${PROJECT_NAME} PUBLIC inc)

# Define the test executable, UnitTest1.
add_executable(UnitTest1 
    unittest/src/unittest.cpp
)

# You may want to link the library under test to your test executable.
target_link_libraries(UnitTest1 PRIVATE ${PROJECT_NAME})

# Add the CTest unit test.
add_test(Test1 UnitTest1)

# Don't need this. As written, it does nothing.
target_link_libraries(${PROJECT_NAME})

I highly encourage you to read through the add_test documentation to understand more of the details and different options that this command provides.

Kevin
  • 16,549
  • 8
  • 60
  • 74
  • thanks. with your suggestins i get an error. there seems something missing, it says: "Start 1: Test1, could not find executable UnitTest1" – user654789384 Aug 10 '20 at 14:42
  • @aphardt This indicates that your executable `UnitTest1` did not successfully build, or that you named it something else. Be sure that the executable target name created in `add_executable()` **matches** the name you reference in the second argument of `add_test`. – Kevin Aug 10 '20 at 14:55
  • ive edited the post with the current results. i think i named it correctly. is there some sort of command i need to execute to "link" the test to get a binary as a result? – user654789384 Aug 10 '20 at 15:01
  • @aphardt In order to run the unit test, you first have to *build* the unit test executable, and be sure it exists on your system. It doesn’t look like you did this, but you might build it using something like `make UnitTest1`. – Kevin Aug 10 '20 at 15:17