Simple ALSA Prgramming problem

Posted on 2004-04-26
Last Modified: 2008-02-01
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;
Question by:soundar110
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5

Accepted Solution

Alf666 earned 200 total points
ID: 10943671
Did you link against the alsa lib at compile time ?

like gcc myprogram.c -o myprogram -lasound


Expert Comment

ID: 10975004
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 ...........

Expert Comment

ID: 10976448
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...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 10976748
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)

Expert Comment

ID: 10976876
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 ?

Author Comment

ID: 10977322
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..........

Expert Comment

ID: 10977698
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.

Author Comment

ID: 10977866
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]

Expert Comment

ID: 10978194
Could you be more specific about the errors ?

What happens at compile time ? Errors ? Please cut/paste.

Author Comment

ID: 10983698
    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............


Expert Comment

ID: 10985196
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 :-)

Author Comment

ID: 10985425
Thanx Very much Alf666


Expert Comment

ID: 10985708
Glad it worked.

Just one question though.

Why a grade of "B" when you got the exact answer to your question ?

Featured Post

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question