"未定义参考"在qt

时间:2018-04-28 19:34:57

标签: c++ qt

我尝试从http://equalarea.com/paul/alsa-audio.html#forget编译样本。我调整了一下,不使用参数。我修复了一个类型转换错误。

我留下了这段代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include </usr/include/alsa/asoundlib.h>
#include </usr/include/alsa/pcm.h>

using namespace std;

int main()
{
        int i;
        int err;
        short buf[128];
        snd_pcm_t *capture_handle;
        snd_pcm_hw_params_t *hw_params;

        const char dev[] = "test";
        if ((err = snd_pcm_open (&capture_handle, dev, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
            fprintf (stderr, "cannot open audio device %s (%s)\n",
                 dev,
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
            fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_any (capture_handle, hw_params)) < 0) {
            fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_access (capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
            fprintf (stderr, "cannot set access type (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_format (capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
            fprintf (stderr, "cannot set sample format (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_rate_near (capture_handle, hw_params, (unsigned int*)44100, 0)) < 0) {
            fprintf (stderr, "cannot set sample rate (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params_set_channels (capture_handle, hw_params, 2)) < 0) {
            fprintf (stderr, "cannot set channel count (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        if ((err = snd_pcm_hw_params (capture_handle, hw_params)) < 0) {
            fprintf (stderr, "cannot set parameters (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        snd_pcm_hw_params_free (hw_params);

        if ((err = snd_pcm_prepare (capture_handle)) < 0) {
            fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
                 snd_strerror (err));
            exit (1);
        }

        for (i = 0; i < 10; ++i) {
            if ((err = snd_pcm_readi (capture_handle, buf, 128)) != 128) {
                fprintf (stderr, "read from audio interface failed (%s)\n",
                     snd_strerror (err));
                exit (1);
            }
        }

        snd_pcm_close (capture_handle);
        exit (0);
}

使用以下代码进行编译时:gcc main.cpp -o alsatest -lasound -lstdc++它的工作方式就像一个魅力,但在编译时,我会得到:

errors

我甚至不会尝试这个但我想让它在qt中运行所以我可以构建一个跨平台的应用程序(在添加对WASAPI的支持之后)。

修改

找到解决方案:在.pro文件中添加LIBS + = -lasound

1 个答案:

答案 0 :(得分:1)

-lasound告诉链接器拉入asound库,以便你的snd _ *()函数调用将解析。您可能没有在QT IDE中指定asound。