You are not logged in.

#1 2006-08-24 13:07:32

alikas
Member
From: Lithuania, Vilnius
Registered: 2006-05-24
Posts: 319
Website

Ho compile program?

How compile little shorted code by me of file 'plsnd.cpp':

#include <stdio>
    #include <stdlib>
    #include <alsa>
          
    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, (unsigned int*)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);
    } 

(http://equalarea.com/paul/alsa-audio.html, "A Minimal Playback Program").
Then I write in terminal and get errors:
[al@myhost ~]$ g++  plsnd.cpp
/tmp/ccNr1Xr9.o: In function `main':
plsnd.cpp:(.text+0x37): undefined reference to `snd_pcm_open'
plsnd.cpp:(.text+0x5b): undefined reference to `snd_pcm_hw_params_malloc'
plsnd.cpp:(.text+0x86): undefined reference to `snd_pcm_hw_params_any'
plsnd.cpp:(.text+0xb9): undefined reference to `snd_pcm_hw_params_set_access'
plsnd.cpp:(.text+0xec): undefined reference to `snd_pcm_hw_params_set_format'
plsnd.cpp:(.text+0x127): undefined reference to `snd_pcm_hw_params_set_rate_near'
plsnd.cpp:(.text+0x15a): undefined reference to `snd_pcm_hw_params_set_channels'
plsnd.cpp:(.text+0x185): undefined reference to `snd_pcm_hw_params'
plsnd.cpp:(.text+0x1a9): undefined reference to `snd_pcm_hw_params_free'
plsnd.cpp:(.text+0x1b4): undefined reference to `snd_pcm_prepare'
plsnd.cpp:(.text+0x1f3): undefined reference to `snd_pcm_writei'
plsnd.cpp:(.text+0x224): undefined reference to `snd_pcm_close'
collect2: ld returned 1 exit status

How compile this code?


Goodbye!

Offline

#2 2006-08-24 13:29:30

alexpnx
Member
From: Nicosia, Cyprus
Registered: 2006-06-10
Posts: 47

Re: Ho compile program?

Hos can compile programs?! LOL

Offline

#3 2006-08-24 13:39:19

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Ho compile program?

--edit--
Whoops.  Think I'm wrong.  You need to include the alsa lib when you compile... I think you need to add -lasound to your compile line:

g++ plsnd.cpp -lasound

(btw, your includes still look weird... tongue)
--end edit--

You don't have the correct includes.

For example, snd_pcm_open is in alsa/pcm.h

Look at the include files in /usr/include/alsa to find out where the missing functions are defined.

Offline

#4 2006-08-24 15:44:10

alikas
Member
From: Lithuania, Vilnius
Registered: 2006-05-24
Posts: 319
Website

Re: Ho compile program?

Thanks!
With '-lsound' compiled.
[al@myhost ~]$ ~/a.out
a.out: pcm.c:2167: snd_pcm_open: Assertion `pcmp && name' failed.


Goodbye!

Offline

Board footer

Powered by FluxBB