How to create a library?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • puneetsardana88
    New Member
    • Aug 2009
    • 57

    How to create a library?

    Hi I have few functions whose definitions I have put up in mycode.c and whose declarations I have put in mycode.h. I want to create a library for this file and for some others also. I want this works similar like math.h or string.h. How can I do so?

    I am using Dev C and Windows XP


    Please help me

    Thank You
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    The header files aren't *really* part of the library.

    Compile your library to a .dll file and then using your linker, link the library to your project.

    You'll have to read about how to link it because it's specific to your compiler/linker.

    Comment

    • puneetsardana88
      New Member
      • Aug 2009
      • 57

      #3
      Compile your library to a .dll file

      How i do that using Dev c++. Can you explain the procedure or provide a link for the same?Regarding linking I can put these library where others libraries are kept.....


      Thank you for the reply

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        A library is a collection of code with no entry point. The code can be shared, as in a DLL (or shared object on Linux) in which case the code is never in the program that links to the library but is loaded by the OS in some fashion. Alternatively the code can be static, part of a static library, this is more like a repository of object files and when I program is linked against a static library those object files are copied out of the library and into the program so the code is in the program.

        In all cases a library consists of a binary file containing the code, a header file declaring the public symbols (functions, classes and variables) in the library and sometimes. In the case of a DLL a much smaller binary file, a static library, also exists containing the code that knows how to load and call the main DLL file. I am not sure if Linux shared objects require something like this too.

        When creating a library you compile you code in the normal fashion, although for a DLL you sometimes need extra directives in the code to indicate which functions will be externally visible to calling programs.

        Where it differs is in the link stage, for a program you call the linker that creates a program, resolving all symbols. For a static library you call a librarian program (or archiver in gcc) that just gathers the object files together into the library without resolving all symbols.

        A DLL is actually an executable image so it too is created with the linker but special switches need to be passed to the linker to tell it to create a DLL.

        However if you are using an IDE (such as Dev-C which, by the way, is somewhat outdated now) then generally it is just a matter of creating a project that creates the correct output, the IDE takes care of calling the correct tools with the correct switches. So for instance to create a library you would create a new project that creates a static library or you would create a new project that creates a DLL.

        Having created the library you should not put it where the other libraries are kept, that directory contains standard libraries and may well be overwritten when a new compiler version comes out. You should leave the standard library directory alone and under the control of the program that created it (not you).

        Again most IDEs allow you to load multiple projects and you can make one project refer to another. If you do this and the project being refered to is a library then many IDEs will automatically link the library into the referring project. This has the advantage that if you build the debug version of you program you get the debug version of the library and if you build the release version of you program you get the release version of your library.

        If your IDE does not do this then it will certainly have a section under the linker configuration of your program where you can specify additional libraries.

        Comment

        • puneetsardana88
          New Member
          • Aug 2009
          • 57

          #5
          Thanks Banfa that was really nice explanation. Thanks a ton.

          Comment

          Working...