title: Calling a Objective C function from C++ Code
body: <p>I googled around and I find a million results to this subject. But none of the pages helps me. I think that I have a very common problem. I'm playing around with audio programming especially working with audio queues. The purpose of my program does not matter for explaining the problem. But in a nutshell: I get an error when I try to call an objective-c function from c++ code. So here is my code that contains the error:
  AudioRecorder.h:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  
  @interface AudioRecorder : NSObject {
  
  }
  
  -(void)setup;
  -(void)startRecording;
  -(void)endRecording;
  -(void)playAlarmSound;
  
  @end
  </code></pre>
  
  <p>And this is the implementation: AudioRecorder.mm:</p>
  
  <pre><code>#import "AudioRecorder.h"
  #include &lt;AudioToolbox/AudioToolbox.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  @implementation AudioRecorder
  
  static const int kNumberBuffers = 3;
  ...
  static void HandleInputBuffer (void                                 *aqData,
                             AudioQueueRef                        inAQ,
                             AudioQueueBufferRef                  inBuffer,
                             const AudioTimeStamp                 *inStartTime,
                             UInt32                               inNumPackets,
                             const AudioStreamPacketDescription   *inPacketDesc ) {
  
      AQRecorderState *pAqData = (AQRecorderState *) aqData;              
  
      if (inNumPackets == 0 &amp;&amp;                                            
          pAqData-&gt;mDataFormat.mBytesPerPacket != 0)
          inNumPackets =
          inBuffer-&gt;mAudioDataByteSize / pAqData-&gt;mDataFormat.mBytesPerPacket;
  
      UInt32 size;
      AudioQueueGetPropertySize ( inAQ, kAudioQueueProperty_CurrentLevelMeter, &amp;size );
      char* levelMeterData = new char[size];
      AudioQueueGetProperty ( inAQ, kAudioQueueProperty_CurrentLevelMeter, levelMeterData, &amp;size );
      AudioQueueLevelMeterState* meterState =  reinterpret_cast&lt;AudioQueueLevelMeterState*&gt;(levelMeterData);
      cout &lt;&lt; "mAveragePower = " &lt;&lt; meterState-&gt;mAveragePower &lt;&lt; endl;
      cout &lt;&lt; "mPeakPower = " &lt;&lt; meterState-&gt;mPeakPower &lt;&lt; endl;
      delete levelMeterData;
      [self playAlarmSound]; //&lt;--- here I get the error: Use of undeclared identifier 'self'   
  
      if (pAqData-&gt;mIsRunning == 0)                                   
          return;
  
      AudioQueueEnqueueBuffer ( pAqData-&gt;mQueue, inBuffer, 0, NULL );
  }
  ...
  -(void)playAlarmSound {
      NSLog(@"Alarmsound....");
  }
  </code></pre>
  
  <p>When I omit "[self playAlarmSound];" then everything works fine. So how do I call this Objective-C function from my C++ code?</p>