Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine if C library is installed on Unix

Tags:

c

unix

As a follow up question to my last one, is there any simple way to tell if a given C library is installed on a given machine (not programattically, just as a once off sort of thing)? I need to use libuuid, but I'm not sure if it's installed on the machines in question. The only two ways I can think of are:

1) Try to compile the code there (more work than I'd like to do)

2) Try something like "man libuuid" although seems like this wouldn't always be reliable if for some reason the manpages didn't get installed.

Is there some better other way?

like image 499
Morinar Avatar asked Sep 03 '09 21:09

Morinar


3 Answers

Have you considered using autoconf? It's designed to check and see whether the build environment is set up correctly.

like image 88
mmx Avatar answered Nov 08 '22 15:11

mmx


The simplest way is to invoke ld with the -l option. This will effectively test the existence of the library, searching the standard library locations automatically:

$ ld -luuid
ld: warning: cannot find entry symbol _start; not setting start address
$ echo $?
0

$ ld -luuidblah
ld: cannot find -luuidblah
$ echo $?
1

# so...
$ ld -luuid 2>/dev/null && echo "libuuid exists" || echo "libuuid not found"

EDIT

As dreamlax pointed out, this does not work for all unix variants. I don't know if it will work on all unixes (I've tested linux and OpenBSD) but you can try this instead:

$ echo "int main(){}" | gcc -o /dev/null -x c - -luuid 2>/dev/null
$ echo $?
0

$ echo "int main(){}" | gcc -o /dev/null -x c - -luuidblah 2>/dev/null
$ echo $?
1
like image 8
mhawke Avatar answered Nov 08 '22 17:11

mhawke


Here's what I did using autoconf, which I'm showing here as a solid example for whoever else might come by next:

I created the file configure.ac which contained the following:

AC_INIT(package, 1.1, email)
AC_CHECK_LIB(uuid, uuid_generate_random, [echo "libuuid exists"], [echo "libuuid missing"])

I then ran the following commands in order (same folder I made configure.ac):

autoconf
./configure

At the end of the configure, it spat back whether or not it had found uuid_generate_random in the uuid library. Seemed to work perfectly (although unfortunately, two of the OSes were missing the library, but that's a whole other problem).

For anybody who may find this after the fact, the AC_INIT arguments here are throwaways and you can copy them wholesale. The arguments to AC_CHECK_LIB are: library name, the name of a function in that library, what to do on success, what to do on failure.

Even though Mehrdad's answer wasn't quite as helpful as I would have liked (i.e. to not have spent time trolling the docs) it seems to be the correct one and I'll be accepting it. mhawke: I really liked your answer, but I wasn't quite sure how to test to make sure it worked. It seemed to on SunOS, but always said no on the other two (AIX, HPUX) and I couldn't seem to come up with a library off the top of my head I could guarantee it would find.

Thanks for the help guys.

like image 2
Morinar Avatar answered Nov 08 '22 16:11

Morinar