Sign in to follow this  

.Lib files?

This topic is 4104 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

I'm creating a small .dll for some code I'll be reusing alot, and I'm not exactly sure how to make the .lib file. As I understand it, I'm supposed to have __declspec(dllexport) before any function that I want to be accessible outside of the DLL? I'm using it like this:
__declspec(dllexport) enum {...};

__declspec(dllexport) void MyFunc();

__declspec(dllexport) extern int MyInt;
Is this correct? My actaul functions don't have '__declspec(dllexport)' before them, only the predeclarations in the header file does. Still, my .lib file isn't being generated, so I'm not sure if I need to do something else or not. The DLL is merely a small wrapper around some SDL functions, so it has SDL included into it. Do I need to do something extra to ensure that it works correctly? It all compiles fine, but there is no .lib file.

Share this post


Link to post
Share on other sites
That is very odd. Normally the EXP and LIB files are generated by default.

This is how I manually do it in MSVC++ .NET 2005:

1) Create new General > Empty Project. Save in, for example, D:\dev\dll_test
2) Open project properties. Go to Configuration Properties > General > Configuration Type. Change to Dynamic Library (.dll)
3) Create C++ source file, place code into file.
4) Compile.

The EXP, LIB and DLL files appear in D:\dev\dll_test\debug (Note: NOT in D:\dev\dll_test\dll_test\Debug as one would assume)

Example code I used:

#include <windows.h>

__declspec(dllexport) void __cdecl Function1(void);

void Function1(void)
{

}


BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
return TRUE;
}




Hope that helps!

Share this post


Link to post
Share on other sites
I did as you said, and still didn't get a .lib file. But perhaps this is becuase of confusion on my part? I get a .def, .o, .a, .dll, .win, and .layout as well as the project file and the source file. .layout has to do with the project's layout, and .win is used when compiling, I believe. Is .a my lib file?

I'm using Dev C++ by the way. And my projects are selected as Win32 DLL.

Share this post


Link to post
Share on other sites
The "problem" is that DEV-C++ does not generate MSVC++ compatible LIB files. I'm not familiar with the mechanics behind DEV-C++, but you could always try linking in the .o or .a files and see if it works.

Otherwise, what you will have to do (as far as I can best estimate), is load the library and its functions at runtime:

1) Use the Win32 API function LoadLibrary to obtain an OS-level handle ID to the DLL file.

2) Use the Win32 API function GetProcAddress to obtain a pointer to your function.

3) At application exit, use the Win32 API function FreeLibrary to release the DLL's handle ID.

This involves a bit of function pointer syntax. The best documentation and example for this that I've found are from MS:

1) Go to msdn.com
2) Search for the documents: "Run-Time Dynamic Linking", "Using Run-Time Dynamic Linking"

So as you can see, the LIB files only really exist as a feature to ease the use of DLL files inside of one's applications, and are absolutely unnecessary.

This process of loading DLL/functions at runtime is actually my preferred method, even though I am using MSVC++. This way if a DLL or function does not exist, I can generate a nice error message instead of those nasty cookie-cutter "The application failed to initialize because it's missing a library file blah blah" messages. :)

Share this post


Link to post
Share on other sites
Here is some example code for loading the DLL/function at runtime, in case the MSDN articles are a bit too obscure:


#include <windows.h>

// handle to DLL file
HMODULE hDLL;

// function pointer prototype
typedef void (*LPMyFunction)(void);

// function pointer
LPMyFunction MyFunction = 0;

// do stuff
BOOL LoadDLL()
{
// 1) Load DLL
// 2) Assign value returned to hDLL
// 3) Check if hDLL is NULL, if so, error and exit
if(0 == (hDLL = LoadLibrary("my_dll.dll")))
{
// failure to load DLL file
return FALSE;
}

// 1) Load function by finding its address in memory
// 2) Assign value returned to the function pointer
// 3) Check if function pointer is NULL, if so, error and exit
if(0 == (MyFunction = (LPMyFunction)GetProcAddress(hDLL, "Function1")))
{
// failure to find function
return FALSE;
}

return TRUE;
}

// undo stuff
void FreeHookDLL()
{
FreeLibrary(hDLL);
}



Share this post


Link to post
Share on other sites
Quote:
Original post by Servant of the Lord
Thanks, I'll check that out. It seems linux can't use .LIB files, so Dev C++ creates .a and .o instead, even though I use windows. Ah well, they ought to work here also. Many thanks.


As far as I know, other Windows compilers may or may not be able to export MS LIB format either. I just wanted to point out that it's more of a MSVC++ than a Windows "thing". :)

Share this post


Link to post
Share on other sites

This topic is 4104 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