.lib files - What is the use??

This is a discussion on .lib files - What is the use?? within the Windows Programming forums, part of the Platform Specific Boards category; Hi! I'm wondering what's the use of the static librarys (.lib files). Could anyone please explain to me how to ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question .lib files - What is the use??

    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!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    70
    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.
    Chintan R Naik

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    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)

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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 ??
    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
    I've compiled this and I got the .lib 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?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    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
    ...viewlexx - julie lexx

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Hmm. I'm not using any of the declspec... stuff.

    Here's one of my SDK headers.
    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
    Please, write me an example of that header that uses declspec stuff.

    Thanks!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    Code:
    //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)
    when you compile the library you need to define
    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
    ...viewlexx - julie lexx

  9. #9
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    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
    ...viewlexx - julie lexx

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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'
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #11
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    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
    Last edited by btq; 06-28-2003 at 11:40 AM.
    ...viewlexx - julie lexx

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Angry

    Oh jesus!
    DAMN!

    Anyone knows how to deal with .lib files (static library)??
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    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
    ...viewlexx - julie lexx

  14. #14
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    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?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  15. #15
    btq
    btq is offline
    julie lexx... btq's Avatar
    Join Date
    Jun 2002
    Posts
    161
    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.
    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 aron@getup.se ...

    /btq
    ...viewlexx - julie lexx

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  3. accessing all files in a folder.
    By pastitprogram in forum C++ Programming
    Replies: 15
    Last Post: 04-30-2008, 10:56 AM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21