Sign in to follow this  

what exactly is in a .lib file?

This topic is 4676 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
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
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
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
Hi!

I've a question about this topic...

If my cpp files have some code like this one:

#ifdef UNICODE
wcscpy(m_wFileName, wFileName);
#else
strcpy(m_wFileName,wFileName);
#endif


is there no need to specify this in the projects settings of my static library project? I mean.. in Project->Settings I only have "General" and "Custom Build" tabs, so I assume it's not necessary, but I'm not really sure.

This is my first project compiled as a library, lol :D

Thanks in advanced!!

Share this post


Link to post
Share on other sites
One thing to keep in mind is when you have a static library, it acts as a normal C++ project when building, so if UNICODE is not defined, it is not linked into the code. So if you try to run your code on a UNICODE platform, then it will fail to work, even though UNICODE is defined. To remedy this, you would need that in the "header file" and your source file must contain both implementations. That or you will need to make two seperate libraries.

As for settings for the project, you *should* have more options than that - it just depends what you have the curent selection on if you are using Visual Studio. If you click on a .cpp file that is part of hte project, you should then gain the Link tab as well, but if not, something may be wrong the the conviguration of your project.

- Drew

Share this post


Link to post
Share on other sites
Ok.. I see. Now I have two configurations, one to generate the UNICODE library version and another one for the non-UNICODE library version.

Everything seems to works fine compiling both of them.

I've been getting link errors when using the library in my application's project, but now it's already solved... lol :D

Thanks for ur help!

[Edited by - crypting on February 23, 2005 1:49:38 PM]

Share this post


Link to post
Share on other sites

This topic is 4676 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this