Simple ALSA Prgramming problem

Hai all.....

    I am new to Linux Programming.......
I am trying to write a simple 'c'  program using ALSA(Advanced Linux Sound Architecture  v1.0.4rc1) Library API......
(ALSA Drivers are installed and works successfully).....And If I try to compile the following pogram i get the following error message........

error message:

/tmp/ccY7c0Dq.o(.text+0x46): In function `main':
: undefined reference to `snd_pcm_open'
collect2: ld returned 1 exit status

Actual code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <getopt.h>
#include <alsa/asoundlib.h>
#include <sys/time.h>

char *device = "plughw:0,0";                    /* playback device */
snd_pcm_format_t format = SND_PCM_FORMAT_S16;   /* sample format */
unsigned int rate = 44100;                      /* stream rate */
unsigned int channels = 1;                      /* count of channels */
unsigned int buffer_time = 500000;              /* ring buffer length in us */
unsigned int period_time = 100000;              /* period time in us */
double freq = 440;                              /* sinusoidal wave frequency in Hz */
int verbose = 0;                                /* verbose flag */
snd_pcm_sframes_t buffer_size;
snd_pcm_sframes_t period_size;
snd_output_t *output = NULL;
int main(int argc, char *argv[])
struct option long_option[] =
                {"help", 0, NULL, 'h'},
                {"device", 1, NULL, 'D'},
                {"rate", 1, NULL, 'r'},
                {"channels", 1, NULL, 'c'},
                {"frequency", 1, NULL, 'f'},
                {"buffer", 1, NULL, 'b'},
                {"period", 1, NULL, 'p'},
                {"method", 1, NULL, 'm'},
                {"verbose", 1, NULL, 'v'},
                {NULL, 0, NULL, 0},
        snd_pcm_t *handle;
        int err, morehelp;
        snd_pcm_hw_params_t *hwparams;
        snd_pcm_sw_params_t *swparams;
        int method = 0;
        signed short *samples;
        unsigned int chn;
        snd_pcm_channel_area_t *areas;
if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
                printf("Playback open error: %s\n",0 );
                return 0;
Who is Participating?
Alf666Connect With a Mentor Commented:
Did you link against the alsa lib at compile time ?

like gcc myprogram.c -o myprogram -lasound

I got a right answer ..........
like u said i should add linker option.............
but it seems that -lasound can't be added...........
instead i am adding -lalsatoss ............
I thought that i am using the OSS/Free emulator for ALSA..............
if so how can i add -lasound ...........
It may be that you have not installed the proper alsa development libraries.

You should have a somewhere.

How did you install alsa ? RPM or tar/configure/make/install ?

It appears that you have multiple EE accounts. This is not right. Moderators will not be very pleased with this...
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

soundar110Author Commented:
I have installed ALSA using RPMs from its native site.
And I can able to see in  \usr\lib directoy......
But i can't able to use that while compiling............
(bcoz  libasound.a file is not there as like libalsatoss.a)
I don't know where you got your RPMs from as alsa does not distribute any.

What you may be missing is the alsa-devel package.

Which Linux distribution are you using ?
soundar110Author Commented:
I am using Redhat 9 (Kernal version 2.4.20)
I have downloaded alsa packages from
I can't able to find the alsa-devel package there.......
could u tell me please where can i find it..........
So, they are not RPMs. They are tar files.

Which means that you have compiled and installed them yourself ?

In this case, it is normal that you don't have a static (.a) library file as the alsa distrib has a small bug in it's libtool scripts.

But you should be able to link against the shared lib.

Please, post the command you use to compile, and the errors you get.
soundar110Author Commented:
Yeh its  all tar files.........
and i installed them by myself.....
it shows no error while i using
   cc test.c -lalsatoss
but if i use -lasound instead of -lalsatoss it shows error.......
I got following result if ia m trying lsmod command .......

Module                  Size  Used by    Not tainted

snd-pcm-oss            44900   0  (autoclean)
snd-mixer-oss          16728   0  (autoclean) [snd-pcm-oss]
snd-intel8x0           24744   0  (autoclean)
snd-ac97-codec         59900   0  (autoclean) [snd-intel8x0]
snd-mpu401-uart         5360   0  (autoclean) [snd-intel8x0]
snd-rawmidi            18720   0  (autoclean) [snd-mpu401-uart]
snd-seq-device          6204   0  (autoclean) [snd-rawmidi]
snd-pcm                84384   0  (autoclean) [snd-pcm-oss snd-intel8x0]
snd-timer              19812   0  (autoclean) [snd-pcm]
snd                    46116   0  (autoclean) [snd-pcm-oss snd-mixer-oss snd-intel8x0 snd-ac97-codec snd-mpu401-uart snd-rawmidi snd-seq-device snd-pcm snd-timer]
gameport                3364   0  (autoclean) [snd-intel8x0]
soundcore               6404   4  (autoclean) [snd]
snd-page-alloc          9300   0  (autoclean) [snd-intel8x0 snd-pcm]
ide-cd                 35708   0  (autoclean)
cdrom                  33728   0  (autoclean) [ide-cd]
agpgart                47776   4  (autoclean)
parport_pc             19076   1  (autoclean)
lp                      8996   0  (autoclean)
parport                37056   1  (autoclean) [parport_pc lp]
autofs                 13268   0  (autoclean) (unused)
3c59x                  30704   1
nls_iso8859-1           3516   1  (autoclean)
nls_cp437               5116   1  (autoclean)
vfat                   13004   1  (autoclean)
fat                    38808   0  (autoclean) [vfat]
keybdev                 2944   0  (unused)
mousedev                5492   1
hid                    22148   0  (unused)
input                   5856   0  [keybdev mousedev hid]
usb-uhci               26348   0  (unused)
usbcore                78784   1  [hid usb-uhci]
ext3                   70784   1
jbd                    51892   1  [ext3]
Could you be more specific about the errors ?

What happens at compile time ? Errors ? Please cut/paste.
soundar110Author Commented:
    This time I can get able to compile without errors (using -lasound itself)........I donno what miracle happened..............
  Dear  Alf666 .......Surely I will give u Whole points ...........
I want just another simple help...........
I got the sample programs from
In that examples I can't able to understand the terms they are using like
period time,period size, stream rate, buffer size,buffer period etc.,I got into many sites to get help ........But I can't able to get the clear idea .......... It would be very helpful to me if U help me in this regard............

You should read the docs in :

I honestly can not help you in this matter as it's related to audio waves formats and I'm not good at this :-)
soundar110Author Commented:
Thanx Very much Alf666

Glad it worked.

Just one question though.

Why a grade of "B" when you got the exact answer to your question ?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.