10

I'm trying to get a demo working writing PCM to the I2S; I've created a small demo program that gives an error when I run with the following command ./demo stereo_small.wav

can't open /dev/mem

So I run with the this command sudo ./demo stereo_small.wav (added sudo) and the demo should now be able to run successfully with elevated privileges.

But I now get this error:

./demo: error while loading shared libraries: libsndfile.so.1: cannot open shared object file: No such file or directory

Initially, to get the program working, I had to execute this command export LD_LIBRARY_PATH=/usr/local/lib just to get it to work, and now the error has returned, but only when I prefix the command with sudo

Please bare with me I've only been using C & Linux for a few hours!

Here's what I've done to compile and run:

gcc `pkg-config --cflags sndfile` -c demo.c
gcc 'pkg-config --libs sndfile' demo.o -o demo
sudo ./demo stereo_small.wav

Update - Cause: some environment variables aren't maintained under the SU context see here

12
  • 1
    The linux tag is probably redundant. It would be better if you provided your distribution instead (probably Debian).
    – Jivings
    Commented Jun 20, 2012 at 23:15
  • Are you sure that libsndfile is where you think it is? Check that directory. Also check the output of echo $LD_LIBRARY_PATH matches it.
    – Jivings
    Commented Jun 20, 2012 at 23:23
  • What are you actually trying to do with /dev/mem?
    – Jivings
    Commented Jun 20, 2012 at 23:24
  • I'm trying to write to the GPIO/I2S outputs! see here: raspberrypi.org/phpBB3/viewtopic.php?f=44&t=8496
    – Dog Ears
    Commented Jun 20, 2012 at 23:30
  • I like to throw myself in at the deep-end!
    – Dog Ears
    Commented Jun 20, 2012 at 23:30

3 Answers 3

4

It's a bit of a hack until I figure out how to compile and link correctly or move the requirement for sudo from the 'finished' code; but here is one option...

run the shell under sudo and add set the environment variable before executing the program:

sudo bash
export LD_LIBRARY_PATH=/usr/local/lib
./demo stereo_small.wav
1
  • Instead of sudo bash you can keep the current shell with sudo -s.
    – Jivings
    Commented Jun 22, 2012 at 9:32
0

If you use su instead of sudo then you can pass a command to the root shell for it to execute:

su --command="export LD_LIBRARY_PATH=/usr/local/lib && ./demo stereo_small.wav"

The problem with using sudo is that the variable we're trying to export does persist across to the root shell. I'm not really sure why.

1
  • 1
    still get the error while loading shared libraries :(
    – Dog Ears
    Commented Jun 22, 2012 at 10:54
0

It is true that sudo "drops" the Environment Variables for security reasons (further reading here).

However, as suggested in this great answer you can pass environment variables along with your call to sudo. In your case, this would look something like this:

sudo LD_LIBRARY_PATH=/usr/local/lib ./demo stereo_small.wav

That is, pass your environment variable(s) in the form VAR=VALUE. This I consider is better as it avoids using su and other "hacky" approaches.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.