Thread: How to properly use a .lib file?

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    74

    How to properly use a .lib file?

    Hello, I have a quick question regarding the usage of a .lib file.

    I have compiled a .dll and am linking to its .lib to try and use it.

    Ex of my DLL contents
    Code:
    __declspec(dllexport) void shareData();
    
    class __declspec(dllexport) Pile
    {
    };
    Now if I have another project and I link to the .lib above what do I need to do to use the class Pile and the function shareData?

    If I just type:
    Code:
    Pile one;
    shareData();
    I get undeclared identifier errors. So I tried:

    Code:
    __declspec(dllimport) void shareData();
    shareData();
    The shareData() function works properly, but how do I access the Pile class?
    Code:
    __declspec(dllimport) class Pile;
    Pile one;
    Above doesn't work.

    Also, I do not understand __declspec(dllimport) because the following codes work identical.
    Code:
    __declspec(dllexport) void shareData();
    shareData();
    Code:
    __declspec(dllimport) void shareData();
    shareData();
    Am I using .lib files completely wrong? I can't find any info on Google for properly using .libs

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You should include your header file with class/function declarations.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    Okay, thanks. Wasn't sure if I needed to do that as I thought perhaps linking to the .lib replaced the need to include the header. I added the header file now and everything seems to be working smoothly

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I also problem on using .lib

    1>main.obj : error LNK2019: unresolved external symbol _MOIRectangle referenced in function _main
    1>d:\codes\motif-apbc\Debug\motif-apbc.exe : fatal error LNK1120: 1 unresolved externals

    how come I can't link up the .lib files? I have added in as new item into my project

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    What do you mean "added in as new item"?

    I usually use something like this:
    header file:
    Code:
    #include <windows.h>
    #ifdef EXPORT_HOOK_FUNCS
      #define HOOK_DECLSPEC __declspec(dllexport)
    #else
      #define HOOK_DECLSPEC __declspec(dllimport)
    #endif
    
    HOOK_DECLSPEC BOOL MyFunction(HWND hAppWnd);
    HOOK_DECLSPEC BOOL AgainMyFunction(HWND hAppWnd);
    And include it in both, the dll and the program.
    DLL:
    Code:
    #define EXPORT_HOOK_FUNCS
    #include "dll.h"
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID){
    	return TRUE;
    }
    
    HOOK_DECLSPEC BOOL MyFunction(HWND hAppWnd) {
    	MessageBox(hAppWnd,"This is me, the DLL","Hi",MB_OK);
    	return TRUE;
    }
    
    HOOK_DECLSPEC BOOL AgainMyFunction(HWND hAppWnd) {
    	MessageBox(hAppWnd,"This is me, the DLL (no 2)","Hi",MB_OK);
    	return TRUE;
    }
    Program (you must link it with the library that was created with the DLL):
    Code:
    #include "dll.h"
    .......
    MyFunction(hwnd);
    .......
    Last edited by maxorator; 09-28-2006 at 04:34 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    How do I link up the dll with the program? Under Microsoft VC, Project settings, under tab link, under object/library modules, i add my lib files but still cannot link


    I try something very simple.
    Below is the code for my file "a.cpp", which I create in Microsoft VC, where I create it under a Win32 Dynamic-Link Library. After compiling it, I copy the .dll and .lib files to the directory of another project, where its file "b.cpp" code is below
    Code:
    __declspec(dllexport) int Multiply(int ParOne,int ParTwo) 
    { 
       int Mlt=ParOne*ParTwo; 
       return Mlt; 
    }

    Code:
    #include "stdafx.h"
    
    __declspec(dllimport)  int Multiply(int ParOne,int ParTwo);
    
    
    int main(int argc, char* argv[])
    {
            Multiply(1,2);
    	return 0;
    }
    Code for "b.cpp". THis is the project which I created underWin32 Console Application. I try to compile it, but got the error

    Linking...
    LINK : fatal error LNK1104: cannot open file "b.dll"
    Error executing link.exe.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I solved my problem! great tutorial http://www.codeproject.com/dll/Dllfun.asp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Replies: 4
    Last Post: 07-06-2006, 02:53 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM