首页 > 解决方案 > 使用 googletest 测试嵌入式 C++ 代码时处理外设寄存器的重复符号

问题描述

我正在使用 C++ 为 MSP430F5529 开发一个爱好项目,并使用 googletest 进行测试。我是 C/C++、微控制器/嵌入式和 googletest 的真正初学者。

MSP 上的外设是通过寄存器控制的,TI 提供了一个<msp430.h>包含处理器特定标头的标头msp5529.h,在我的情况下,该标头定义了大量的位常量等,但它还声明了微控制器上可用的寄存器,例如UCA1CTL1使用设置串行通信。

例如,当我为构建<msp430.h>进行编译时UART.h,一切都按预期工作。但是,在测试时,我想包含一个<msp430.h>我们可以调用的可测试版本testable_msp430.h

所以基本上我们有以下内容:

UART.h

#ifndef TESTING
#include <msp430.h>
#else 
#include "testable_msp430.h"
#endif

testable_msp430.h

int UCA1CTL1;

*A bunch of other declarations*

test_UART.cpp

#include UART.h

*A bunch of tests*

UART.cpp

#include UART.h

*A bunch of source*

问题是,当通过运行编译它进行测试时,g++ -std=c++11 src/UART.cpp test/test_UART.cpp -lgtest -lgtest_main -pthread -o testOutput -DTESTING我得到一个链接错误说明

duplicate symbol _UCA1CTL1 in:
    /var/folders/yr/mkwg3mhs1nl93l35x6t55vz80000gn/T/UART-772fab.o
    /var/folders/yr/mkwg3mhs1nl93l35x6t55vz80000gn/T/test_UART-2e1a90.o

这是有道理的,因为在和UCA1CTL1的编译单元中都定义了. 因此,我的问题是通常如何处理内存映射寄存器以便能够测试/使用它们?UART.cpptest_UART.cpp

提前致谢!

标签: c++embeddedgoogletest

解决方案


您在 testable_msp430.h 中有定义而不是声明。而不是:

int UCA1CTL1;

你应该有:

extern int UCA1CTL1;

然后在另一个仅链接到测试或包装在的翻译单元中#ifndef TESTING,或在 test_UART.cpp 中,放置et al的单个测试定义。UCA1CTL1


推荐阅读