Hi!
I'm wondering what's the use of the static librarys (.lib files).
Could anyone please explain to me how to use this files and what are the benefits?
Thanks!
Printable View
Hi!
I'm wondering what's the use of the static librarys (.lib files).
Could anyone please explain to me how to use this files and what are the benefits?
Thanks!
Functions whose declaration you find in header files, definition of those functions are there in .lib files.
Defaultly when you create application VC++ adds needed .lib files itself. Sometimes we have to add it ourself.
For example:
Suppose you have create an dll using VC++ IDE. So it will create .dll file along with a .lib file. Now whenever you want to use this .dll at compile time you have to provide .lib file also. Otherwise linker will not get function definition and will report error.
Ok.
You know, I'm creating my own SDK and I think that I really need to create this .lib file. Beacuse if I create a new project and I want to use my SDK, than I must include .c files from this SDK to my project (which is using SDK). And this is kinda annoying. I just want to include the .lib file so the user who will be using my SDK won't have to include .c files from SDK.
So, how to create this .lib files?
You can just create a static library project in VC++ (or whatever compiler you use). The resulting lib file will hold all the implementation of your code so others can link your functions into their project without needing or seeing the implementation (the .c files)
I'm using VC++ . NET.
I've created a static library project and now I have two files, stdafx.cpp and stdafx.h.
Code:// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
# pragma once
# define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// TODO: reference additional headers your program requires here
# include <windows.h>
# include <time.h>
# include "colors.h"
# include "dialogbox.h"
# include "button.h"
# include "translate.h"
# include "cge.h"
// Is this right ??
I've compiled this and I got the .lib file.Code:// stdafx.cpp : source file that includes just the standard includes
// Library.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
But if I now want to compile this .lib file with my project that is using SDK it says that functions that I am using cannot be linked.
This is the error message:
"Button error LNK2019: unresolved external symbol _Print referenced in function _main
"
And yes, I did include my .lib file. What is wrong?
are you sure you're exporting/importing it right ?,
ie, using the "_declspec(dllexport) / _declspec(dllimport)" correct ?
what does the header look like where the functions are declared ?
/btq :)
Hmm. I'm not using any of the declspec... stuff.
Here's one of my SDK headers.
Please, write me an example of that header that uses declspec stuff.Code:# ifndef BUTTON_H
# define BUTTON_H
// structure for the BUTTON component
typedef struct BUTTON
{
short x, y, NoOfItems, Default, Value;
BOOL Selected, *Clicked, Horizontal;
WORD Color, HLColor, DisabledItemColor, DisabledItemHLColor;
char **Items;
struct
{
UINT InputCodePage, OutputCodePage;
} CODEPAGE;
} BUTTON_COMPONENT;
// function prototype
BUTTON_COMPONENT *ButtonComponent (BUTTON_COMPONENT *Button, INPUT_RECORD ir, short Initialize, short NoOfComp);
# endif // BUTTON_H
Thanks!
when you compile the library you need to defineCode:
//this goes in the header
#ifdef export
#define MYLIBAPI extern "C" _declspec(dllexport)
#else
#define MYLIBAPI extern "C" _declspec(dllimport)
#endif
MYLIBAPI int MyFunctionInDaLib(int,int)
MYLIBAPI int AnotherFunctionInDaLib(int,int)
export: "#define export" , before the header file. This exports these functions.
When you're not defining export you import them instead :)
and then you're all set...
/btq :)
ehhrmm...I should've just done this in the first place, sorry about that :
Code:
# ifndef BUTTON_H
# define BUTTON_H
#ifdef export
#define MYLIBAPI extern "C" _declspec(dllexport)
#else
#define MYLIBAPI extern "C" _declspec(dllimport)
#endif
// structure for the BUTTON component
typedef struct BUTTON
{
short x, y, NoOfItems, Default, Value;
BOOL Selected, *Clicked, Horizontal;
WORD Color, HLColor, DisabledItemColor, DisabledItemHLColor;
char **Items;
struct
{
UINT InputCodePage, OutputCodePage;
} CODEPAGE;
} BUTTON_COMPONENT;
// function prototype
MYLIBAPI BUTTON_COMPONENT *ButtonComponent (BUTTON_COMPONENT *Button, INPUT_RECORD ir, short Initialize, short NoOfComp);
# endif // BUTTON_H
/btq :)
Hmm, are you sure that this has to be done for .lib files? It looks like that this works for .dll files.
This is not working. I get an error at this line
MYLIBAPI BUTTON_COMPONENT *ButtonComponent (BUTTON_COMPONENT *Button, INPUT_RECORD ir, short Initialize, short NoOfComp); // syntax error: 'string'
the .lib files only tells the compiler where the functions reside
inside the dll. You must put the dll inside your programs library.
There are no code in the .lib file, it's just an import library.
edit::Ohh sorry, didn't realise you were makin a STATIC library..
I'm soo sorry :( . Never done static-libraries like that I just use
dll's and link to them statically using the .lib file.
/btq :)
Oh jesus!
DAMN!
Anyone knows how to deal with .lib files (static library)??
I'm really sorry about pointing you in the wrong direction :(
It's really hard to tell what's wrong without seeing the code
(or rather the project ).
Can you upload the soultion/projects ?
/btq
It's OK.
Project is too big. Source code is around 0.5 MB.
Look, I know how to compile the .lib file. That is OK. But than the problem occurs when I include my .lib file into my project which is using headers of the SDK project (source code is around 0.5MB). It looks like that those .c files from SDK project are not compiled. I thought that the compiled source is in the .lib file. Isn't that so?
yes, it's like that.Quote:
It looks like that those .c files from SDK project are not compiled. I thought that the compiled source is in the .lib file. Isn't that so?
How do you include the .lib file ? "Add existing" or in
"project properties->linker->inputs->Additional dependencies" ?
Don't know what the difference is really, both of the ways seem
to work here, but try to copy the .lib into the solution dir
of you project using the SDK-headers and add it to the additional dependencies-field. Might do the trick although it seems like
a long shot.
if you want you can mail me the code at [email protected] ...
/btq
I already added .lib file into the project. And the program accepts this file. I know this for sure beacuse if I rename the file into something that does not exists, than the linker says that this file cannot be found. So .lib file is in, but the linker still says for external error, cannot find Print function.
DAMN!
hmmm..when you said
,does this apply to all functions, ie, you can't use a single functionQuote:
This is the error message:
"Button error LNK2019: unresolved external symbol _Print referenced in function _main"
from you lib ?
like I said it's really hard to solve this without seeing the code.
I suggest you take a look at your build-log and assure yourself
that your app actually links with the correct .lib file
(but maybe you've already done this ?).
/btq :)