我收到以下错误: “符号SBPTest乘以定义(由../../build/typeFind.LPC1768.o和../../ build / main.LPC1768.o)。”
我在common.h中声明了SBPTest,如下所示:
#ifndef COMMON_H_
#define COMMON_H_
extern RawSerial SBPTest(USBTX, USBRX);
#endif
其他文件看起来像这样...
typeFind.h:
#include "mbed.h"
#include <string>
extern unsigned int j;
extern unsigned int len;
extern unsigned char myBuf[16];
extern std::string inputStr;
void MyFunc(char* inputBuffer);
typeFind.cpp:
#include "typeFind.h"
#include "common.h"
void MyFunc(char* inputBuffer) {
inputStr = inputBuffer;
if (inputStr == "01") {
len = 16;
for ( j=0; j<len; j++ )
{
myBuf[j] = j;
}
for ( j=0; j<len; j++ )
{
SBPTest.putc( myBuf[j] );
}
}
}
main.cpp:
#include "typeFind.h"
#include "common.h"
#include "stdlib.h"
#include <string>
LocalFileSystem local("local"); // define local file system
unsigned char i = 0;
unsigned char inputBuff[32];
char inputBuffStr[32];
char binaryBuffer[17];
char* binString;
void newSBPCommand();
char* int2bin(int value, char* buffer, int bufferSize);
int main() {
SBPTest.attach(&newSBPCommand); //interrupt to catch input
while(1) { }
}
void newSBPCommand() {
FILE* WriteTo = fopen("/local/log1.txt", "a");
while (SBPTest.readable()) {
//signal readable
inputBuff[i] = SBPTest.getc();
//fputc(inputBuff[i], WriteTo);
binString = int2bin(inputBuff[i], binaryBuffer, 17);
fprintf (WriteTo, "%s\n", binString);
inputBuffStr[i] = *binString;
i++;
}
fprintf(WriteTo," Read input once. ");
inputBuffStr[i+1] = '\0';
//fwrite(inputBuff, sizeof inputBuffStr[0], 32, WriteTo);
fclose(WriteTo);
MyFunc(inputBuffStr);
}
char* int2bin(int value, char* buffer, int bufferSize)
{
//..................
}
我正在mbed LPC1768上编程。串行用于main.cpp和typeFind.cpp。我已经看过堆栈溢出,建议使用common.h文件,但出现编译器错误。
答案 0 :(得分:1)
您没有在标头中定义变量,否则最终会在所有包含标头的翻译单元中定义变量,这违反了一个定义规则。只声明它:
// common.h
extern RawSerial SBPTest;
并仅在一个源文件中进行定义:
// common.cpp (or any other, but exactly one source file)
RawSerial SBPTest(USBTX, USBRX);
我建议使用列表初始化或复制初始化,因为直接初始化语法与函数声明不明确,可能会使任何不知道USBTX
和USBRX
是类型还是值的人感到困惑:< / p>
// common.cpp (or any other, but exactly one source file)
RawSerial SBPTest{USBTX, USBRX}; // this
auto SBPTest = RawSerial(USBTX, USBRX); // or this