0

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...

6
  • it's worth noting that the original code is for C, but i'm using it in c++. i just renamed the .c file to .cpp, and changed some of the pointers from void to float, as it didn't seem to like pointer arithmetic with void pointers...
    – JimmyB
    Jul 10, 2011 at 23:36
  • Try removing the inline from the source file. Inlined functions really only belong in header files.
    – Kerrek SB
    Jul 10, 2011 at 23:37
  • 4
    @JimmyB : I'd strongly recommend using Boost.CircularBuffer instead of shoehorning in this poor C code.
    – ildjarn
    Jul 10, 2011 at 23:42
  • thanks ildjarn, I'll check the boost library out now!
    – JimmyB
    Jul 10, 2011 at 23:48
  • @ildjarn i'd love to use boost, but i can't get it to install...
    – JimmyB
    Jul 11, 2011 at 12:40

1 Answer 1

6

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

1
  • that worked great - i included that code and renamed the source file back to TPCircularBuffer.c (from .cpp) and it builds fine. thank you!
    – JimmyB
    Jul 10, 2011 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.