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

icktoofay
  • 126,289
  • 21
  • 250
  • 231
JimmyB
  • 326
  • 3
  • 12
  • 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 '11 at 23:36
  • Try removing the `inline` from the source file. Inlined functions really only belong in header files. – Kerrek SB Jul 10 '11 at 23:37
  • 4
    @JimmyB : I'd **strongly** recommend using [`Boost.CircularBuffer`](http://www.boost.org/doc/libs/release/libs/circular_buffer/index.html) instead of shoehorning in this poor C code. – ildjarn Jul 10 '11 at 23:42
  • thanks ildjarn, I'll check the boost library out now! – JimmyB Jul 10 '11 at 23:48
  • @ildjarn i'd love to use boost, but i can't get it to install... – JimmyB Jul 11 '11 at 12:40
  • @JimmyB : Boost.CircularBuffer is header-only, there's nothing to "install" -- you just need the header files. – ildjarn Jul 11 '11 at 15:38

1 Answers1

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

nos
  • 223,662
  • 58
  • 417
  • 506
  • 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 '11 at 23:44