LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-07-2011, 06:09 AM   #1
Martin R
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Rep: Reputation: Disabled
undefined reference to snd_pcm_open snd_sterror ...


Hello Community,
I'm newbie in Linux and want to write an application to play audiodata from a buffer.
I startes to write a Test Application using the example I found im web.
http://home.roadrunner.com/~jgglatt/tech/linuxapi.htm
Code:
#include <stdio.h>
#include <string.h>
#include <alsa/asoundlib.h>
int main(int argc, char **argv)
{
   register int  err;
   int           cardNum;
   // Start with first card
   cardNum = -1;
   for (;;){
      snd_ctl_t *cardHandle;
      if ((err = snd_card_next(&cardNum)) < 0){
         printf("Can't get the next card number: %s\n", snd_strerror(err));
         break;
      }
      if (cardNum < 0) break;
      // Open this card's control interface. We specify only the card number -- not
      // any device nor sub-device too
      {
       char   str[64];
       sprintf(str, "hw:%i", cardNum);
       if ((err = snd_ctl_open(&cardHandle, str, 0)) < 0){
         printf("Can't open card %i: %s\n", cardNum, snd_strerror(err));
         continue;
       }
      }

      {
      snd_ctl_card_info_t *cardInfo;
      // We need to get a snd_ctl_card_info_t. Just alloc it on the stack
      snd_ctl_card_info_alloca(&cardInfo);
      // Tell ALSA to fill in our snd_ctl_card_info_t with info about this card
      if ((err = snd_ctl_card_info(cardHandle, cardInfo)) < 0)
         printf("Can't get info for card %i: %s\n", cardNum, snd_strerror(err));
      else
         printf("Card %i = %s\n", cardNum, snd_ctl_card_info_get_name(cardInfo));
      }
      // Close the card's control interface after we're done with it
      snd_ctl_close(cardHandle);
   }
   // ALSA allocates some mem to load its config file when we call some of the
   // above functions. Now that we're done getting the info, let's tell ALSA
   // to unload the info and free up that mem
   snd_config_update_free_global();
}
if I compile it I get a lot of errors:

main.cpp:16: Error:undefined reference to `snd_card_next'
main.cpp:17: undefined reference to `snd_strerror'
main.cpp:26: undefined reference to `snd_ctl_open'
main.cpp:27: undefined reference to `snd_strerror'
main.cpp:36: undefined reference to `snd_ctl_card_info_sizeof'
main.cpp:39: undefined reference to `snd_ctl_card_info'
main.cpp:40: undefined reference to `snd_strerror'
main.cpp:42: undefined reference to `snd_ctl_card_info_get_name'
main.cpp:46: undefined reference to `snd_ctl_close'
main.cpp:51: undefined reference to `snd_config_update_free_global'


I also include <alsa/controls.h> . but I am not able to compile the code :-(

.

thanks for your help.

Martin

Last edited by Martin R; 11-07-2011 at 09:32 AM.
 
Old 11-07-2011, 09:33 AM   #2
Martin R
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Compiler flag -lasound
 
Old 07-07-2014, 03:18 AM   #3
chuppi
LQ Newbie
 
Registered: Jan 2013
Location: bengaluru
Posts: 3

Rep: Reputation: Disabled
<alsa/controls.h>

its <alsa/control.h> and compile
 
Old 06-14-2017, 05:04 AM   #4
DimaRabadi
LQ Newbie
 
Registered: Jun 2017
Posts: 1

Rep: Reputation: Disabled
Unhappy Same problem

I am also facing the same problem regarding alsa, I added the required <alsa/control.h> file but still the same problem. Any help plz?
 
Old 11-20-2018, 05:50 AM   #5
WillingLearner
LQ Newbie
 
Registered: Nov 2018
Posts: 1

Rep: Reputation: Disabled
Hi, go onto your project on eclipse, right click and select "Properties", once there, expand "C/C++ Build" and select "Settings". On the right under the "Tool Settings" tab, select "GCC C Linker" and select "Libraries". On the "Libraries(-l)" field, click on the green + icon for adding libraries and type "asound". Under the "Library search path(-L)", click on the green + icon for adding search path and type in "/usr/lib/asound". Then apply and close, it should link properly afterwards. Remember to add the compiler flag "-lasound" when compiling.
 
Old 07-28-2020, 11:27 AM   #6
SketchMastah
LQ Newbie
 
Registered: Jul 2020
Posts: 1

Rep: Reputation: Disabled
Where are you putting the -lasound flag? It would seem that in recent times, the order of the arguments passed to the compiler have come to matter. Some old tutorial will tell you how to compile, you'll do it their way, and get this "undefined reference" error.

Assuming you are using g++,
Instead of writing something like:
g++ -lasound -o path/to/executable path/to/source.cpp

Write this:
g++ -o path/to/executable path/to/source.cpp -lasound
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Yet another undefined reference CollieJim Linux - General 2 12-17-2010 11:08 PM
undefined reference to.... crapodino Programming 1 01-13-2008 07:05 PM
undefined reference to... dimah Programming 3 12-27-2006 09:57 AM
undefined reference? Sharky01252 Programming 3 11-07-2006 11:36 AM
Undefined Reference ChemicalBurn Programming 2 02-14-2005 03:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration