How does the compiler use the lib file?

I am wondering how the c / C ++ compiler parses lib files? I mean, I am creating a library containing some classes, I use this library in my main program. How does the compiler know what class names exist in this library. Of course, this information is in binary format, I want to use this functionality in my program to be specific. I have a binary lib file and I want to know all the classes and properties / functions present in this lib file.

Is it possible? If the compiler can do this, why can't there be some kind of library?

thanks for any hint

+4
source share
4 answers

The compiler does not do what you offer, but the linker does.

The compiler knows the necessary information from the header files that are included in the lib files.

The linker then combines the declarations you include and the lib file along with other object files.

You can probably get the information from the .lib file by decompiling it, for example, but this is a lot of work and probably not what you want to do.

+6
source

Depending on your system (I know this is true for .a files on Linux, not so for Windows). Library files are just an archive file (thus .a ) containing a bunch of normal object files. Think if you can or zip and you will be damn close. The compiler more or less simply extracts files from the archive, captures them on the command line and links everything together (in fact, this is not so because it has some special rules, for example, only lazy things, but it is close enough for the topic at hand )

If you want to know what's inside, grab the library tool (IIRC ar on Linux) and take a look at the docs. Even money says that the tool can extract object files, and from there you can use standard tools to extract their contents.

+1
source

Typically, a .a library is simply an archive that itself contains a bunch of object files. These commands are executed on Linux; they will vary between platforms. You can usually view the main contents of the archive by running nm libxxx.a

 /usr/lib> nm libc.a | head -10 init-first.o: U abort U _dl_non_dynamic_init 0000000000000000 T _dl_start w _dl_starting_up U __environ U __fpu_control U __init_misc 0000000000000004 C __libc_argc 

The nm man page has many details on what exit means.

The ar command can extract individual files for you, for example.

 ~/tmp> ar x libc.a init-first.o ~/tmp> file init-first.o init-first.o: ELF 32-bit LSB relocatable, Intel 80386, version 1 (SYSV), not stripped 

As you can see above, the objects here are the ELF format, so for the complex bit of what you are asking, you really need something that can learn the ELF binaries. Linux usually comes with the readelf command, which will give a lot of information on the command line:

 ~/tmp> readelf -s init-first.o Symbol table '.symtab' contains 19 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000000 0 SECTION LOCAL DEFAULT 1 2: 00000000 0 SECTION LOCAL DEFAULT 3 3: 00000000 0 SECTION LOCAL DEFAULT 4 4: 00000000 0 SECTION LOCAL DEFAULT 5 5: 00000000 0 SECTION LOCAL DEFAULT 6 6: 00000000 11 FUNC GLOBAL DEFAULT 1 _dl_start 7: 00000000 0 NOTYPE GLOBAL DEFAULT UND abort 8: 00000010 133 FUNC GLOBAL DEFAULT 1 __libc_init_first 9: 00000000 0 NOTYPE WEAK DEFAULT UND _dl_starting_up ... 

There is a libelf library that very likely does what you want and accesses this information programmatically. I never used it, since I always got the necessary information from the available command line tools.

http://directory.fsf.org/wiki/Libelf

+1
source

To reinforce Brian R. Bondi ’s response to the moment the code got into the linker, the most useful information for people was deleted. 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 really less useful. The library documentation should explain why you would like to use it and how; if the library is not well documented, you may need a better library.

0
source

Source: https://habr.com/ru/post/1305908/


All Articles