what exactly is in a .lib file?

Started by
12 comments, last by crypting 19 years, 1 month ago
ok. I undrstand the concept of linking include files into your project such as d3d9.h. but what is the purpose of linking d3d9.lib also? Are there extra functions inside of the .lib files that the programmers didn't want to put into the .h files?
Advertisement
Header files (such as d3d9.h) contain only the function prototypes. The function code itself is in the .lib file. The header file is used during compiling and informs the compiler that a certain function you are calling exists in another module. The linker will then go looking for the function, which it finds in the .lib file.

ok. but some header files also come with .cpp files. the function declarations are in the headers and the implementation is in the .cpps. so why create library files to implement the functions when you could just write a corresponding .cpp file?
For various reasons.. One reason is that a .lib file can contain the code from many .cpp files.. so it's a way to batch together a large amount of code in a single library (thus the extension .lib). Another reason is that by providing a .lib file you give the user the functionality, but not the source code - so Microsoft can protect their DX code in this case (well, actually most of the DX .lib files are just "stub" loaders for corresponding .dll files).

- Kasper
ah, ok. that makes sense. thanks for the info![smile]
There alsp another reason: if you use the cpp file and not the corresponding lib, compiling can become very slow. Libraries are often well tested code, and are used without modify it: so there are no reasons why you should recompile it's code...
Also it allows the .lib file to be updated without everyone (that wants to be uptodate) having to go and change the cpp files they are using.
Quote:Original post by cyberninjaru

ok. I undrstand the concept of linking include files into your project such as d3d9.h. but what is the purpose of linking d3d9.lib also? Are there extra functions inside of the .lib files that the programmers didn't want to put into the .h files?


No offense, but it seems you don't understand the concept - even if you state the contrary.

First, you don't link with .h files. .h files gives enough informations to the compiler in order to enable it to compile your program. It says, for exemple, that this particular structure contains this particular member. .h files contains the declaration of the symbols you wan to use.

.cpp files usually implements these declaration in order to make the definitions. This is really two different things, because the declaration - by itself - do not produce code, only the definition does.

When the compiler sees a declaration, it 'thinks' : "this things look lie this". When it sees a definition, it says "it acts like this".

Up to this point, you don't speak about linking. .h and .cpp files are not linked together - you can declare things that will not be deined, and you can define things that were not declared before.

There is a big interest in having the two separated: since the compiler only needs a declaration to understand how the things look like, you don't have to provide a definition for the symbol you used in your module if such definition exist in another module. This is where linking enters the game, because a using a symbol means you must be able to link your to this symbol. If the symbol is defined in the same compilation unit, no problem, because the compiler wil be able to find it easily. If this is not the case, then the compiler is not able to resolve the problem because it compiles only one file at a time. Once all the .cpp are compiled, the linker tool is invoked to resolve all these issues.

.lib files are a collection of already compiled compilation units (.cpp). Therefore, they contain the compiled definition of the declared symbols.

When you want to use an external library, the library typically contains a buch of .h files and a .lib file. The .h files allows the compiler to know what the library contains. The .li files allow the linker to find the defintions of teh defined symbols. This is exactly the same as providing both .cpp and .h, except that the source code is already compiled - and therefore you can't have a look or modify it.

Hope I make all this stuf clear :)

Regards,
Thanks for degrating my character Emmanuel but I understand what you're saying. so, here's another question. how do I take all of the functions in my .cpp file and convert them into a library?
Quote:Original post by cyberninjaru
how do I take all of the functions in my .cpp file and convert them into a library?


If you are using something like Visual Studio, you can create a new project that is either a static library or a dynamic link library. If you want the fast and easy way, you should take the static library method. All you do is copy over all the .cpp files and the .h files, build and you are done. You end up with one giant .lib file that contains everything. The .dll way is a little bit more tricker, so I suggest taking the static lib route first so you can get used to making them. After you have your .lib and .h file, you just copy over to a new project, link the .lib, include the .h and your up and running.

- Drew

This topic is closed to new replies.

Advertisement