首页 > 解决方案 > Xcode 找不到 PortAudio 的标签“错误”

问题描述

我正在尝试按照Initialising PortAudio 教程中的描述初始化 portaudio 。

它说要检查初始化过程中是否有错误,如下所示:

PaError err = Pa_Initialize();
if (err != paNoError) goto error;

这是我正在使用的确切代码。

我在 OS X Mojave 10.14.4 上运行它,使用 Xcode 10.1 和 10.12 OS X SDK。

我试图找到 PortAudio 文档中的错误标签无济于事,并且名为error.

到目前为止的完整程序是:

# include <iostream>
# include "portaudio.h"
using namespace std;

// Typedef and demo callbacks here.

int main(int argc, const char * argv[])
{
    PaError err = Pa_Initialize();

    if (err != paNoError) goto error;

    // Nothing here yet.

    err = Pa_Terminate();

    if (err != paNoError)
    {
        printf("Port audio error terminating: %s", Pa_GetErrorText(err));
    }
    return 0;
}

据我在教程中可以看出,这应该是一个有效的语句,但 Xcode 显示语法错误: Use of undeclared label 'error'

标签: c++xcodeportaudio

解决方案


检查goto 语句的 c++ 参考 PortAudio 的示例程序,问题来自假设goto可以访问 portaudio.h 文件中定义的内容,但事实并非如此。

如果您遇到此问题,我假设您也不熟悉goto语句。

本教程假设主要功能中有一部分专门用于解决错误。为了解决这个问题,我们需要在我们的main函数中定义一个错误标签来负责响应错误。

例如:

int main(void) {
    PaError err;

    // Checking for errors like in the question code, including goto statement.

    return 1; // If everything above this goes well, we return success.

error:               // Tells the program where to go in the goto statement.
    Pa_Terminate();  // Stop port audio. Important!
    fprintf( stderr, "We got an error: %s/n", Pa_GetErrorMessage(err));
    return err;    
}

推荐阅读