tags:

views:

357

answers:

2

Given a C++/UNIX library file (without extension), i need to determine the type of library whether it is a dynamic library (.so file) or a static library (.a file) based on its content (say grepping the content against a keyword)

How do i do it in UNIX command line ?

+5  A: 

Tryfile <library name>. It should displayshared or dynamically linked among its output if the file is a dynamically loadable module.

Aviator
Hi,Thanks for the reply. Your answer is perfectly fine. But i wanted to know how to proceed with the approach of 'grep'ping the library content for some keyword and determine the library type (if that's possible).
Arvind K
Why do you want to grep instead of use the file command? The file command basically does this and has the magic numbers and other characteristics for hundreds of different file types in its database. Whenever possible, let someone else maintain things like this for you.
Omnifarious
+1 I was waiting for the write up.
Preet Sangha
@Arvind: Like Omnifarious asked, why do you need to `grep` and go for complex way when you have a simple `file` command :). Is there any specific reason that you are not wanting `file`?
Aviator
+1  A: 

Try file -L <library name> | grep shared if this produces any output, the file is dynamically linked. Alternately you could do ldd <library name> | grep 'not a dynamic executable' which produces output if it's static. Hope this answers your question, I would have added a comment to Aviator's answer, but I cannot comment (yet).

The -L option to file forces files to dereference symlinks, which is not the default behavior if POSIXLY_CORRECT is not defined (as it is the case on my system).

Script example:

if [ -z "$(file -L  | grep shared)" ]; then
    echo "not a dynamic lib";
else
    echo "dynamic lib";
torque
file -L option does not work in solaris OS.
Arvind K