c++apimbedlpc

How to define an interface/API which is used in multiple cpp files?


I get the following error: "Symbol SBPTest multiply defined (by ../../build/typeFind.LPC1768.o and ../../build/main.LPC1768.o)."

I have declared SBPTest in common.h like this:

#ifndef COMMON_H_
#define COMMON_H_

extern RawSerial SBPTest(USBTX, USBRX);

#endif

Other files look like this...

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)
{
    //..................
}

I am programming on mbed, a LPC1768. The serial is used both in main.cpp and typeFind.cpp. I have looked on stack overflow and a common.h file is recommended, yet I get a compiler error.


Solution

  • You musn't define the variable in the header, or else you end up defining it in all translation units that include the header, which violates the one definition rule. Only declare it:

    // common.h
    extern RawSerial SBPTest;
    

    And define in exactly one source file:

    // common.cpp (or any other, but exactly one source file)
    RawSerial SBPTest(USBTX, USBRX);
    

    I recommend using either list initialisation or copy initilisation, since direct initilisation grammar is ambiguous with a function declaration and may confuse anyone who doesn't know whether USBTX and USBRX are types or values:

    // common.cpp (or any other, but exactly one source file)
    RawSerial SBPTest{USBTX, USBRX};        // this
    auto SBPTest = RawSerial(USBTX, USBRX); // or this