Thread: Creating a LIB

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    Creating a LIB

    I was wondering if someone could explain to me how to create a LIB. I want to make 1 so that I can combine all my source and header files into 1 easy to add lib file, its getting very annoying to add lots of headers and source files.

    Also, if you make a LIB file to contain all your headers and source files, do you only have to link the LIB file to use the functions?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There's a "Win32 Static Library" project type in VC++ 6.0 (and probably .NET as well).

    For GNU tools: http://www.google.com/search?sourcei...tic+library+ar

    gg

  3. #3
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Also, if you make a LIB file to contain all your headers and source files, do you only have to link the LIB file to use the functions?
    I believe you still need the header files to call the functions you want.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    So basically i create the project as Codeplug described, i include all the source files, i compile it, link it to my project, include my headers, and itll work?

  5. #5
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    In a nutshell yes.

    Though this is what I normally do when I create a static lib. I create a separate project to create my static lib. After testing that my functions work as intended, I build my static lib.

    Then in my project that uses the static lib, I include the header files and link to the static lib.

    All this isn't necessary, but I find it easier to debug big projects.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    cool, but do i need to include my header files in the static library too? they contain all the declerations for the source files and i dont think itll compile without them, so i have to link to something that already contains my header files and then include the header files again?

    [btw sorry for the lack of grammer and/or puncuation(spelt right?), its just that im under a time limit so I gotta type fast , but its good to know you guyz were online to help me out ]
    Last edited by X PaYnE X; 01-08-2004 at 11:37 AM.

  7. #7
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Here's a step-by-step description of how I build static libs.

    Let's say I wanted to create a static lib that contains a function: println(char*). I'll use only 1 function in my example for simplicity's sake. This function just prints a string and adds a carriage return at the end.

    1. I create a new Static Library project. How you do this depends on what IDE you're using. Both MSVC and DevC++ support static library projects. MSVC generates .lib files and DevC++ generates .a files. Let's just say the output is printline.lib

    2. I create this header file.

    Code:
    #ifndef _PRINTLINE_H_
    #define _PRINTLINE_H_
    #include <stdio.h>
    
    void println( char* c ) ;
    
    #endif
    3. I create the implementation.

    Code:
    #include "println.h"
    
    void println( char* c )
    {
      printf( "%s\n", c ) ;
    }
    4. I build the project. Your static library should be created (printline.lib or whatever).

    5. Now I create a new project and create my main source file that uses my println() function. Notice it includes the println.h file.

    Code:
    #include "println.h"
    
    int main(int argc, char *argv[])
    {
      println( "This is a line!" ) ;
      
      getchar() ;
    
      return 0;
    }
    6. Link the printline.lib file and build your project.
    Last edited by Dante Shamest; 01-08-2004 at 11:45 AM.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    It all depends.
    If the header contains definitions that are needed by both the lib and programs that use the lib, then yes.

    Try it out!
    If you hit a road-block, post back.

    gg

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    Guys, how can I ever repay you? Youve helped me out in 2 issues which youve explained perfectly.

    I thank you again and anyone else who helped out, you have helped me succeed in my goal: custom controls.

    And Dante, if theres anything I can do to repay your kindness, ill do it, besides i have nothing else to do, ive finished my goal for the month.

    And Codeplug, same goes for you, youve helped me out in countless other issues

    Thanks for all your help!

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Another rule of thumb I like to follow:
    - Only #include within a file (header or source) if that file needs definitions from the #include

    So in Dante's example, I would suggest moving the "#include <stdio.h>" from println.h and into println.c and main.c since those are the only files that are using stuff out of stdio.h.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. using VC6 link VC7 static lib error
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2006, 10:58 PM
  3. Creating a lib (HELP)
    By Captain_Bunny in forum Linux Programming
    Replies: 2
    Last Post: 12-21-2005, 04:12 AM
  4. creating a lib file?
    By Devil Panther in forum C Programming
    Replies: 8
    Last Post: 11-01-2003, 08:18 AM
  5. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM