简体   繁体   English

使用CppUnit或类似的交叉编译单元测试

[英]Cross compiling unit tests with CppUnit or similar

Has anyone used a package like CppUnit to cross-compile C++ unit tests to run on an embedded platform? 有没有人使用像CppUnit这样的包来交叉编译C ++单元测试以在嵌入式平台上运行?

I'm using G++ on a Linux box to compile executables that must be run on a LynxOS board. 我在Linux机器上使用G ++来编译必须在LynxOS板上运行的可执行文件。 I can't seem to get any of the common unit test packages to configure and build something that will create unit tests. 我似乎无法获得任何常见的单元测试包来配置和构建将创建单元测试的东西。

I see a lot of unit test packages, CppUnit, UnitTest++, GTest, CppUTest, etc., but very little about using these packages in a cross-compiler scenario. 我看到了很多单元测试包,CppUnit,UnitTest ++,GTest,CppUTest等,但在交叉编译器场景中使用这些包很少。 The ones with a "configure" script imply that this is possible, but I can't seem to get them to configure and build. 带有“configure”脚本的脚本意味着这是可能的,但我似乎无法让它们进行配置和构建。

./configure --prefix=/sandBox --build=`config.guess` --host=sh4-linux

sh4-linux是您要运行程序的平台。

You might want to look at CxxTest . 你可能想看看CxxTest I have not used it for cross compilation, but it is based entirely on headers and a Python script - no compiled library. 我没有用它来进行交叉编译,但它完全基于头文件和Python脚本 - 没有编译库。 It might be easier to adapt than others. 它可能比其他人更容易适应。

My practice when unit testing code that is cross compiled is to compile the unit tests themselves using the native toolchain -- usually some flavor of x86 compiler. 单元测试交叉编译的代码时,我的做法是使用本机工具链编译单元测试 - 通常是x86编译器的某种风格。 These unit tests execute on the build machine rather than on the embedded target. 这些单元测试在构建机器上执行,而不是在嵌入式目标上执行。 If you're writing strict unit tests (as opposed to integration tests) with stubs and mocks you shouldn't have dependencies on embedded hardware. 如果您使用存根和模拟编写严格的单元测试(而不是集成测试),则不应该依赖嵌入式硬件。 If not... it's never too late to start. 如果不是......它永远不会太晚开始。

One added benefit of this approach is that for non-x86 embedded targets, this type of unit testing helps flush out endianness issues, uninitialized variables and other interesting bugs. 这种方法的另一个好处是,对于非x86嵌入式目标,这种类型的单元测试有助于清除字节序问题,未初始化的变量和其他有趣的错误。

To cross-compile CppUTest (v3.3), I had to override the LD, CXX and CC make variables. 为了交叉编译CppUTest(v3.3),我不得不重写LD,CXX和CC make变量。

To get both the CppUTest and CppUTestExt (for CppUMock) libraries and their tests built I used the following commands from the CPPUTEST_HOME directory: 为了获得CppUTest和CppUTestExt(用于CppUMock)库及其构建的测试,我使用了CPPUTEST_HOME目录中的以下命令:

To build libCppUTest.a: 要构建libCppUTest.a:

make all LD=sh4-linux-g++ CXX=sh4-linux-g++ CC=sh4-linux-gcc

To build libCppUTestExt.a (for CppUMock): 要构建libCppUTestExt.a(用于CppUMock):

make extensions LD=sh4-linux-g++ CXX=sh4-linux-g++ CC=sh4-linux-gcc

You can then copy the CppUTest_tests and CppUTestExt_tests executables that are produced in your CPPUTEST_HOME to your target device and execute them there. 然后,您可以将CPPUTEST_HOME中生成的CppUTest_tests和CppUTestExt_tests可执行文件复制到目标设备并在那里执行它们。

Assuming CppUTest passes it's own tests on your target, you're then ready to develop your tests with CppUTest. 假设CppUTest在您的目标上传递了它自己的测试,那么您就可以使用CppUTest开发测试了。 Just link your test code with the cross-compiled CppUTest libraries and copy the resulting executable to your target. 只需将测试代码与交叉编译的CppUTest库链接,然后将生成的可执行文件复制到目标。 Then run to get unit test results from the target platform itself. 然后运行以从目标平台本身获取单元测试结果。

Im not providing an answer here, but i wouldn't take the advice of NOT running your unit tests on different targets : you still need to, preferably both system and unit tests. 我没有在这里提供答案,但我不会接受不在不同目标上运行单元测试的建议:你仍然需要,最好是系统和单元测试。

Otherwise simple things like alignment errors on ARM/other embedded CPUs will not get caught. 否则,ARM /其他嵌入式CPU上的对齐错误之类的简单事情将不会被捕获。

It sounds like you need to have unit test library compiled for your OS and architecture as well as what's on your dev/build machine(s). 听起来你需要为你的操作系统和架构以及dev / build机器上的内容编译单元测试库。 I prefer Boost++ unit test framework for this. 我更喜欢Boost ++单元测试框架。 You can either download something that's prebuilt for your architecture, but will usually have to compile it yourself. 您可以下载为您的体系结构预先构建的内容,但通常必须自己编译。 I found a few solutions by googling for how to cross-compile boost (eg http://goodliffe.blogspot.com/2008/05/cross-compiling-boost.html ). 我通过谷歌搜索找到了一些解决方案,如何交叉编译提升(例如http://goodliffe.blogspot.com/2008/05/cross-compiling-boost.html )。 CppUnit might be easier to cross-compile, haven't tried. CppUnit可能更容易交叉编译,还没试过。 The general principle is the same, you compile the same library version for your development architecture and for your target machine 一般原则是相同的,您为开发体系结构和目标计算机编译相同的库版本

My setup for new targets is to compile the necessary Boost++ libraries for my target OS/arch and then write tests to link against both Boost++ libraries and the code to be tested. 我对新目标的设置是为我的目标OS / arch编译必要的Boost ++库,然后编写测试以链接Boost ++库和要测试的代码。

The benefit is that you can link against your x86 Linux Boost++ libs or against your target Boost++ libs, thus you can run the tests on both your target and your dev/build machine(s). 好处是您可以链接到x86 Linux Boost ++库或目标Boost ++库,因此您可以在目标和开发/构建机器上运行测试。

My general setup looks like this: 我的常规设置如下所示:

libs/boost/<arch>/<boost libs>
src/foo.{cpp,h}
tests/test_foo.cpp
build/foo
build/test_foo.<arch>

I put compiled Boost++ libs under different architectures that I need in libs/ dir for all my projects and reference those libs in my Makefiles. 我把编译好的Boost ++库放在libs / dir所需的不同架构下,用于我的所有项目,并在我的Makefiles中引用这些库。 The source and the tests get build with an arch variable specified to make command that way I can run test_foo.x86 on my dev machine and test_foo.{arm,mips,ppc,etc.} on my targets. 源代码和测试使用指定的arch变量进行构建,以便我可以在我的开发机器上运行test_foo.x86并在我的目标上运行test.foo。{arm,mips,ppc等}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM