I am fairly new to Linux and ALSA. I am trying to compile a simple program to enumerate sound devices on a Raspberry Pi 4 running the Raspberry Pi OS based on Debian Bullseye. The code (which I copied from an example on the web) is:
#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);
char **hints;
/* Enumerate sound devices */
int err = snd_device_name_hint(-1, "pcm", (void***)&hints);
if (err != 0)
return 0;//Error! Just return
char** n = hints;
while (*n != NULL) {
char *name = snd_device_name_get_hint(*n, "NAME");
if (name != NULL && 0 != strcmp("null", name)) {
//Copy name to another buffer and then free it
free(name);
}
n++;
}//End of while
//Free hint buffer
snd_device_name_free_hint((void**)hints);
return 0;
}
I installed the ALSA libraries using
$ sudo apt install libasound
$ sudo apt install libasound2
$ sudo apt install libasound-dev
when I try to compile, I get:
$ gcc -lasound test1.c -o test1
/usr/bin/ld: /tmp/ccUWxojL.o: in function `main':
test1.c:(.text+0x28): undefined reference to `snd_device_name_hint'
/usr/bin/ld: test1.c:(.text+0x60): undefined reference to `snd_device_name_get_hint'
/usr/bin/ld: test1.c:(.text+0xb8): undefined reference to `snd_device_name_free_hint'
collect2: error: ld returned 1 exit status
so it seems that the linker is finding the library, but can't find the functions that I need.
I checked to make sure that libasound.so is in /usr/lib/arm-linux-gnueabihf, and then I checked to make sure that those functions were present, which they were:
$ nm -D /usr/lib/arm-linux-gnueabihf/libasound.so | grep snd_device_name*
0002f358 T snd_device_name_free_hint@@ALSA_0.9
0002f898 T snd_device_name_get_hint@@ALSA_0.9
0002f394 T snd_device_name_hint@@ALSA_0.9
Anyway, I'm not sure where to go from here. Any help would be appreciated. Thanks.