Results 1 to 3 of 3
i am a newbie to linux and not to c am trying to write a program using the alsa api the code does not compile and gives the same errors
...
- 08-29-2008 #1Just Joined!
- Join Date
- Apr 2008
- Posts
- 1
help alsa api in c
i am a newbie to linux and not to c am trying to write a program using the alsa api the code does not compile and gives the same errors
Code:[tanmayat@localhost sound prog]$ gcc -o types types.c /home/tanmayat/tmp/cczBMf66.o: In function `main': types.c:(.text+0x42): undefined reference to `snd_pcm_stream_name' types.c:(.text+0x7c): undefined reference to `snd_pcm_access_name' types.c:(.text+0xb6): undefined reference to `snd_pcm_format_name' types.c:(.text+0xc5): undefined reference to `snd_pcm_format_description' types.c:(.text+0xd2): undefined reference to `snd_pcm_format_name' types.c:(.text+0x110): undefined reference to `snd_pcm_subformat_description' types.c:(.text+0x11d): undefined reference to `snd_pcm_subformat_name' types.c:(.text+0x15b): undefined reference to `snd_pcm_state_name' collect2: ld returned 1 exit status
and the code is from a web hosting i was just trying to have a look around
Code:#include <stdio.h> #include <stdlib.h> #include </usr/include/alsa/asoundlib.h> main (int argc, char *argv[]) { int i; int err; short buf[128]; snd_pcm_t *playback_handle; snd_pcm_hw_params_t *hw_params; if ((err = snd_pcm_open (&playback_handle, argv[1], SND_PCM_STREAM_PLAYBACK, 0)) < 0) { fprintf (stderr, "cannot open audio device %s (%s)\n", argv[1], 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 (playback_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 (playback_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 (playback_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 (playback_handle, hw_params, 44100, 0)) < 0) { fprintf (stderr, "cannot set sample rate (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params_set_channels (playback_handle, hw_params, 2)) < 0) { fprintf (stderr, "cannot set channel count (%s)\n", snd_strerror (err)); exit (1); } if ((err = snd_pcm_hw_params (playback_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 (playback_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_writei (playback_handle, buf, 128)) != 128) { fprintf (stderr, "write to audio interface failed (%s)\n", snd_strerror (err)); exit (1); } } snd_pcm_close (playback_handle); exit (0); }
- 08-29-2008 #2
It's the linking part that goes wrong, not the compiling.
You have correctly included the header files but you did not tell the linker in which library it can find the actual code for declared functions. I do not know how the library for ALSA is called but you need to pass it with the -l$LIBRARYNAME option. (Note the additional 'l' and that there is no space between)
- 10-05-2008 #3Just Joined!
- Join Date
- Oct 2008
- Posts
- 2
use gcc filename -l asound option


Reply With Quote