You are not logged in.
Pages: 1
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
Hos can compile programs?! LOL
Offline
--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... )
--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
Thanks!
With '-lsound' compiled.
[al@myhost ~]$ ~/a.out
a.out: pcm.c:2167: snd_pcm_open: Assertion `pcmp && name' failed.
Goodbye!
Offline
Pages: 1