开发者

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

开发者 https://www.devze.com 2023-03-19 10:47 出处:网络
I\'m using the \'TPCircularBuffer\' class for creating a circular buffer object, from this website. This is my current code:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号