Problem compiling ALSA project’s pcm.c sample in raspberry pi zero

  alsa, c++, libalsa, raspberry-pi

I am using a raspberry pi zero W with the latest raspberry pi OS

I have installed both libasound2 and libasound2-dev, and this code that I got off the raspberry pi forums for testing the alsa library compiles and runs correctly:

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

int main() {
  int val;

  printf("ALSA library version: %sn", SND_LIB_VERSION_STR);

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

  return 0;
}  

I am trying to compile the sinewave generation sample code provided in the ALSA project’s website here: https://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_8c-example.html

This is listed as pcm.c in their examples page

I have saved this code in main.cpp and I have changed #include "../include/asoundlib.h" (which causes a file not found error on compile) to #include <alsa/asoundlib.h>

I compile this using gcc main.cpp -o main -lasound -fpermissive. I get a series of warnings but finally an error at line for (format = 0; format < SND_PCM_FORMAT_LAST; format++) { where it says there is no match for operator++ and the operand type is 'snd_pcm_format_t' {aka '_snd_pcm_format'}). But format has been declared as static snd_pcm_format_t format = SND_PCM_FORMAT_S16; and snd_pcm_format_t is an enum as per their documentation.

What do I need to do to get this basic example to compile? Or is there a simpler example showing how to generate a sine tone in ALSA using C++, or some blog or tutorial explaining how this sample code by ALSA works?

Source: Windows Questions C++

LEAVE A COMMENT