tags:

views:

85

answers:

3

I am curious about how c/c++ compiler analyse lib files ? I mean say I create a library containing some classes, I am using that library in my main program. How does compiler know what class names are there in that library. Of course that information is present in binary format, I want to use that functionality in my program, to be specific I have a binary lib file and I want to know all classes and properties/functions present in that lib file.

Is it possible ? If compiler can do that why can't some library ?

thanks for any clue

+4  A: 

The compiler doesn't do what you are suggesting, but the linker does.

The compiler knows the information it needs from the header files that are included relating to the lib files.

The linker then puts together the declarations you are including and the lib file along with its other object files.

It is probably possible to get the information from the .lib file by decompiling it for example, but this is a lot of work and probably not what you're looking to do.

Brian R. Bondy
Can we use that functionality in our programs ? Is there any library which does job of linker for knowing class names and its properties ?
Xinus
@Xinux: Possibly but I'm not aware of them.
Brian R. Bondy
A: 

To amplify Brian R. Bondy's answer by the time the code has hit the linker, the most useful information to humans has been stripped out. For example, you can extract the following information from an object file (library):

class SomeClass implements SomeInterface {
    int get(class &otherClass c, float x);
    bool reallyCoolMethod(int x);
    ...
}

Which is truly less than helpful. The library documentation should explain why you would want to use it and how; if the library isn't so documented, you might want a better library.

msw
I don't think C++ has interfaces :\
Billy ONeal
+1  A: 

Depending on your system (I know this is true for .a files on Linux, not so sure for windows) library files are just an archive file (thus the .a) containing a bunch of normal object files. Think tar or zip and you will be darn close. The linker more or less just extracts the files form the archive, tacks them in the command line and links everything together (It doesn't actually do that because it has some special rules like only getting things lazily, but it's close enough for the topic at hand).

If you want to know what inside, get library tool (IIRC ar on Linux) and dig into the docs. Even money says the tool will be able to extract the object files and from there you can use standard tools to extract their contents.

BCS