tags:

views:

424

answers:

3

Hello,

I wrote a C app that uses the PCRE library. Everything works on my own computer. However, when I copy the binary over to another computer and run it, it gives the following error:

/libexec/ld-elf.so.1: Shared object "libpcre.so.0" not found, required by "myapp"

I know I can probably get it to work by installing the PCRE lib on the target computer. However, I'm wondering if there's a more convenient way of doing this? (just copying over a few lib files?)

I tried to copy over the libpcre.so.0 file, but it didn't work.

Any help is appreciated! Thanks,

+2  A: 

It's better to either install it or link it in statically. The former, of course, is lighter on resources. The best way to ensure compatibility would be to build the package for target system, specifying all dependencies (depends on the distribution, of coursE).

Michael Krelin - hacker
How can I link pcre in statically? I'm pretty bad at compiling c/c++
Dave
There's a number of ways. You can link the whole think statically, providing `-static` option to `gcc` or you can specify the libpcre.a library instead of -lpcre (or whatever it is in your command line). Actually, it depends on your build machinery.
Michael Krelin - hacker
A: 

You should be able to copy it and then set the envvar LD_LIBRARY_PATH to the folder where it exists, or even create a shell script that sets this envvar then launches your program as follows

LD_LIBRARY_PATH=. ./your_program

Check the Program Library How To

Diaa Sami
Yup, it's one possible way. But compiling it statically seems more ideal for my situation
Dave
+1  A: 

As @hacker said, you either have to ensure that you install PCRE on the target machine or you have to use a static library (libpcre.a instead of libpcre.so) on the development machine. That might also mean you need to build PCRE with a static library, and you'd have to use the correct compile time options to pull in the static library. One relatively easy way to do it is to specify /usr/lib/libpcre.a on the compiler command line. Ideally, you'd avoid including -lpcre on the command line too - certainly, you'd want the static library to appear ahead of the shared library.

Your copy may have failed because of issues with symlinks. You usually link to a file such as:

/usr/lib/libpcre.so

but that is a symlink to a versioned library such as:

/usr/lib/libpcre.so.0

Or it could work the other way around. If you were using tar to copy things, you may have copied a symlink.

Ideally, you install PCRE in a system directory - but doing that requires root privileges. You also have to be careful to ensure that you don't overwrite a more recent version of PCRE with your older version. You also want to avoid forcing users into setting the LD_LIBRARY_PATH environment variable (or its equivalents), or forcing them to use the configuration program (ld.so.conf?).

Jonathan Leffler
Compiling it statically seems the best for my situation since I don't have root privileges on the target computer. however, I'm not able to get the compile lines working. I included the -static flag and got rid of lpcre but now it won't find the lib file (gives me a bunch of pcre functions as undefined errors).
Dave
try replacing -lpcre with /usr/lib/libpcre.a (or wherever your libpcre.a is)
Michael Krelin - hacker
When Jonathan talks about avoiding -lpcre he certainly doesn't mean removing it without proper substitution.
Michael Krelin - hacker
still the same errors =/
Dave
Do you actually have a static PCRE library on your build machine. You probably don't - hence the "can't find the library" error. And, quite likely, the default build of PCRE only builds shared libraries. (Having said that, PCRE 5.0 - circa 2004 - defaulted to both static and shared libraries.)
Jonathan Leffler
I do have libpcre.a in /usr/local/lib
Dave
and the errors are something like "undefined reference to `pcre_fullinfo'"
Dave
The current version of PCRE is 7.3 (5.0 was ancient)...it to builds both static and shared libraries by default. However, you may need to actually install that.
Jonathan Leffler
Can you give us your command line?
Michael Krelin - hacker
The installed PCRE here is 7.9
Dave
I'm using a Makefile: http://pastebin.com/d2cd7bd81
Dave
Try `$(CC) $(CFLAGS) -o $@ $^ /usr/local/lib/libpcre.a` instead.
Michael Krelin - hacker
Yup =) that was the solution. The lib file needs to be placed after the .c file. Thanks!
Dave
You're welcome ;-)
Michael Krelin - hacker
Thanks for finishing the job, @hacker - I had to pretend to get some work done, sadly. (And you already got a +1 from me.)
Jonathan Leffler
Also - regarding the version 7.3 vs 7.9; I collected 7.3 from ftp.pcre.org earlier today, but if you search for the library, you can indeed find that version 7.9 is available at a new official home ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ or via SourceForge at http://sourceforge.net/projects/pcre/files/
Jonathan Leffler