在树莓派零操作系统中编译ALSA项目的pcm.c示例时出现问题

在树莓派零操作系统中编译ALSA项目的pcm.c示例时出现问题

我使用的是树莓派 0 W 和最新的树莓派操作系统

我已经安装了 libasound2 和 libasound2-dev,以及我从树莓派论坛用于测试 alsa 库是否正确编译和运行:

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

int main() {
  int val;

  printf("ALSA library version: %s\n", SND_LIB_VERSION_STR);

  printf("\nPCM stream types:\n");
  for (val = 0; val <= SND_PCM_STREAM_LAST; val++)
    printf("  %s\n",
      snd_pcm_stream_name((snd_pcm_stream_t)val));

  return 0;
}  

我正在尝试编译 ALSA 项目网站中提供的正弦波生成示例代码:https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_8c-example.html

这在示例页面中列为 pcm.c

我已将此代码保存在 main.cpp 中,并且已更改为#include "../include/asoundlib.h"(这会导致编译时出现文件未找到错误)#include <alsa/asoundlib.h>

我使用 编译这个gcc main.cpp -o main -lasound -fpermissive。我收到一系列警告,但最终在显示for (format = 0; format < SND_PCM_FORMAT_LAST; format++) {no match for operator++operand type is 'snd_pcm_format_t' {aka '_snd_pcm_format'}).但format已被宣布为static snd_pcm_format_t format = SND_PCM_FORMAT_S16;snd_pcm_format_t是一个枚举根据他们的文档

我需要做什么才能编译这个基本示例?或者是否有一个更简单的示例展示如何使用 C++ 在 ALSA 中生成正弦音,或者有一些博客或教程解释 ALSA 的示例代码如何工作?

编辑:这些是构建过程中看到的消息:[错误是main.cpp:842:66: 错误:与“operator++”不匹配]

$ gcc main.cpp -o Main -lasound -fpermissive
main.cpp:19:23: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
 static char *device = "plughw:0,0";         /* playback device */
                       ^~~~~~~~~~~~
main.cpp: In function ‘int write_and_poll_loop(snd_pcm_t*, short int*, snd_pcm_channel_area_t*)’:
main.cpp:314:18: warning: invalid conversion from ‘void*’ to ‘pollfd*’ [-fpermissive]
     ufds = malloc(sizeof(struct pollfd) * count);
            ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp: In function ‘void async_callback(snd_async_handler_t*)’:
main.cpp:397:77: warning: invalid conversion from ‘void*’ to ‘async_private_data*’ [-fpermissive]
 ct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

main.cpp: In function ‘void async_direct_callback(snd_async_handler_t*)’:
main.cpp:469:77: warning: invalid conversion from ‘void*’ to ‘async_private_data*’ [-fpermissive]
 ct async_private_data *data = snd_async_handler_get_callback_private(ahandler);
                               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~

main.cpp: In function ‘void help()’:
main.cpp:756:53: warning: invalid conversion from ‘int’ to ‘snd_pcm_format_t’ {aka ‘_snd_pcm_format’} [-fpermissive]
                 const char *s = snd_pcm_format_name(k);
                                                     ^
In file included from /usr/include/alsa/asoundlib.h:54,
                 from main.cpp:14:
/usr/include/alsa/pcm.h:1065:56: note:   initializing argument 1 of ‘const char* snd_pcm_format_name(snd_pcm_format_t)’
 const char *snd_pcm_format_name(const snd_pcm_format_t format);
                                 ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~
main.cpp: In function ‘int main(int, char**)’:
main.cpp:842:27: warning: invalid conversion from ‘int’ to ‘snd_pcm_format_t’ {aka ‘_snd_pcm_format’} [-fpermissive]
             for (format = 0; format < SND_PCM_FORMAT_LAST; format++) {
                           ^
main.cpp:842:66: warning: no ‘operator++(int)’ declared for postfix ‘++’, trying prefix operator instead [-fpermissive]
             for (format = 0; format < SND_PCM_FORMAT_LAST; format++) {
                                                            ~~~~~~^~
main.cpp:842:66: error: no match for ‘operator++’ (operand type is ‘snd_pcm_format_t’ {aka ‘_snd_pcm_format’})
main.cpp:903:21: warning: invalid conversion from ‘void*’ to ‘short int*’ [-fpermissive]
     samples = malloc((period_size * channels * snd_pcm_format_physical_width(format)) / 8);
               ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:909:19: warning: invalid conversion from ‘void*’ to ‘snd_pcm_channel_area_t*’ {aka ‘_snd_pcm_channel_area*’} [-fpermissive]
     areas = calloc(channels, sizeof(snd_pcm_channel_area_t));
             ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$  

使用 g++ 而不是 gcc 会产生相同的错误

答案1

您正尝试将其编译为 C++ 程序(“ISO C++ 禁止...”)。因此,您会收到各种警告,因为包含的标头是 C,而不是 C++。

要么重命名main.cppmain.c,以便将其编译为 C 程序,要么使用正确的 C++ 方式将 C 标头包含到extern "C",然后使用g++,并确保您自己的代码遵循 C++ 标准。

相关内容