我们开始与Zephyr合作,并决定使用C++。我现在试图弄清楚如何对代码进行单元测试。据我了解,已经集成到zephyr / nrfconnect中的ztest和unity不支持c ++。我决定使用googletest。我创建了一个自定义模块来下载,编译和集成到zephyr。我现在有一些麻烦。
我的项目的结构如下所示:
|-modules
| |-googletest
| |-zephyr
| |-module.yml
| |-CMakeLists.txt
| |-CMakeLists.txt.in
| |-Kconfig
|-src
|-tests
|-button
| |-main.cpp
|-CMakeLists.txt
|-prj.conf
|-testcase.yaml
CMake测试/按钮列表如下所示:cmake_minimum_required(VERSION 3.13.1)
set(BOARD native_posix)
set(ZEPHYR_TOOLCHAIN_VARIANT zephyr)
set(TOOLCHAIN_ROOT ${ZEPHYR_BASE})
set(ZEPHYR_EXTRA_MODULES $ENV{ZEPHYR_BASE}/../iot_aleph/modules/googletest)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project("Button unit tests")
# Include event headers
zephyr_library_include_directories(../../src/board)
# Add test sources
target_sources(app PRIVATE src/main.cpp)
target_sources(app PRIVATE ../../src/board/button.cpp)
target_link_libraries(app PUBLIC gtest_main)
模块/ googletest的CMakeLists.txt如下所示:cmake_minimum_required(VERSION 3.1)
if(CONFIG_GOOGLETEST)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
endif()
CMakeLists.txt.incmake_minimum_required(VERSION 3.1)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.10.0
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
GoogleTest框架似乎可以正确编译,但是在进行链接时,我有很多错误:[92/93] Linking CXX executable zephyr/zephyr.elf
FAILED: zephyr/zephyr.elf zephyr/zephyr.lst zephyr/zephyr.stat zephyr/zephyr.exe
: && ccache /usr/bin/g++ zephyr/CMakeFiles/zephyr_prebuilt.dir/misc/empty_file.c.obj -o zephyr/zephyr.elf -Wl,-T zephyr/linker.cmd -Wl,-Map=/home/cyril/Documents/zephyr_workpsace/iot_aleph/tests/button/build/zephyr/zephyr_prebuilt.map -Wl,--whole-archive app/libapp.a zephyr/libzephyr.a zephyr/arch/arch/posix/core/libarch__posix__core.a zephyr/soc/posix/inf_clock/libsoc__posix__inf_clock.a zephyr/boards/posix/native_posix/libboards__posix__native_posix.a -Wl,--no-whole-archive zephyr/kernel/libkernel.a zephyr/CMakeFiles/offsets.dir/arch/posix/core/offsets/offsets.c.obj -L/home/cyril/Documents/zephyr_workpsace/iot_aleph/tests/button/build/zephyr lib/libgtest_main.a lib/libgtest.a -lpthread -Wl,--gc-sections -Wl,--build-id=none -Wl,--sort-common=descending -Wl,--sort-section=alignment -Wl,-u,_OffsetAbsSyms -Wl,-u,_ConfigAbsSyms -lstdc++ -m32 -ldl -pthread -lm && cd /home/cyril/Documents/zephyr_workpsace/iot_aleph/tests/button/build/zephyr && cmake -E rename zephyr_prebuilt.map zephyr.map && /usr/bin/objdump -S zephyr.elf > zephyr.lst && /usr/bin/readelf -e zephyr.elf > zephyr.stat && /usr/bin/cmake -E copy zephyr.elf zephyr.exe
/usr/bin/ld: i386:x86-64 architecture of input file `lib/libgtest.a(gtest-all.cc.obj)' is incompatible with i386 output
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)':
...
gtest-all.cc:(.text+0x25bb): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::AssertionResult::AssertionResult(testing::AssertionResult const&)':
gtest-all.cc:(.text+0x2876): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::internal::(anonymous namespace)::SplitEscapedString(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
gtest-all.cc:(.text+0x41d6): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x41f4): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x4250): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x427c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::substr(unsigned long, unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x42bc): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/usr/bin/ld: gtest-all.cc:(.text+0x42f0): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::substr(unsigned long, unsigned long) const'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::internal::StringStreamToString(std::__cxx11::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >*)':
gtest-all.cc:(.text+0x6c0a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::reserve(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::Test::Test()':
gtest-all.cc:(.text+0x7b75): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: lib/libgtest.a(gtest-all.cc.obj): in function `testing::TestInfo::TestInfo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, char const*, char const*, testing::internal::CodeLocation, void const*, testing::internal::TestFactoryBase*)':
gtest-all.cc:(.text+0x86b7): undefined reference to `operator new(unsigned long)'
/usr/bin/ld: gtest-all.cc:(.text+0x8726): undefined reference to `operator new(unsigned long)'
...
有人能帮我吗?西里尔
最佳答案
赶上@MatzZze!
我已经添加了:
set(CMAKE_C_FLAGS -m32)
set(CMAKE_CXX_FLAGS -m32)
现在一切都在编译并运行!