Jump to content
  • Advertisement
Sign in to follow this  
cyberninjaru

what exactly is in a .lib file?

This topic is 5042 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!