c++ - 使用 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.cpp
test_UART.cpp
提前致谢!
解决方案
您在 testable_msp430.h 中有定义而不是声明。而不是:
int UCA1CTL1;
你应该有:
extern int UCA1CTL1;
然后在另一个仅链接到测试或包装在的翻译单元中#ifndef TESTING
,或在 test_UART.cpp 中,放置et al的单个测试定义。UCA1CTL1
推荐阅读
- mysql - 使用的 SELECT 语句在 codeigniter 中有不同数量的列
- css - Mediawiki:我需要提示如何调试»遇到的异常,类型为“ParseError”«
- c++ - 如何防止 googletest 从我的测试代码中吞下 stdout (std::cout)?
- python - 将一个df的一列连接到另一个df的所有列
- powershell - 仅在文本文件中从第 5 列中删除空格
- git - 如何从本地 git 服务器克隆特定的 Git 分支
- watchkit - iwatch 并发症未在表盘并发症列表中列出,但出现在 iphone 的并发症部分
- javascript - 如何使情况真实 JAVASCRIPT
- c++ - 使用 Xlib 获取应用程序的图标?
- ios - 我可以阻止删除 ios mdm 配置文件吗?