Search code examples
c++circular-bufferundefined-symbol

Able to instantiate an object but not access its functions - 'Symbol(s) not found' error


I'm using the 'TPCircularBuffer' class for creating a circular buffer object, from this website. This is my current code:

 TPCircularBufferRecord bufferRecord;                   //1
 TPCircularBufferInit(&bufferRecord, kBufferLength);    //2

Line 1 works fine, so that means the linker has found the .cpp and .h files for the class. However, line 2 doesn't compile, with the error:

Undefined symbols:
  "TPCircularBufferInit(TPCircularBufferRecord*, int)", referenced from:
      StereoEffect3::StereoEffect3(ComponentInstanceRecord*)in StereoEffect3-1DB483EC8D75827.o
      StereoEffect3::StereoEffect3(ComponentInstanceRecord*)in StereoEffect3-1DB483EC8D75827.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I don't think it's a problem with the original source code, but I'll include it here anyway: TPCircularBuffer.c

TPCircularBuffer.h

Does anyone know why the linker/compiler can't find the function TPCircularBufferInit? The TPCircularBufferInit function is as follows:

 inline void TPCircularBufferInit(TPCircularBufferRecord *record, int length) {
     record->head = record->tail = record->fillCount = 0;
     record->length = length;
 }

I'm pretty sure I'm passing the correct type of arguments into it...


Solution

  • You're mixing and matching C and C++ code.

    The TPCircularBuffer.h isn't C++ safe, so when you include it in your C++ source, it'll get treated as C++. That'll fail, in this case only at link time. The linker will look for C++ name mangeled symbols for the cicrular buffer functions, but TPCircularBuffer.c is compiled as C code.

    Just do this where you include the TPCircularBuffer.h header:

    extern "C" {
    #include "TPCircularBuffer.h"
    };
    

    Alternativly, this code should be ok when compiled as C++ too, so just rename the TPCircularBuffer.c file to TPCircularBuffer.cpp