Thread: Making library

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    197

    Making library

    I have designed a program which deals about stack and its functionalities...

    Code:
    Just an overview of the design of the code:
    
    struct STACK{
           int value;
           struct STACK * next;
    };
    
    function defined are : insert,pop, etc..
    main()
    {
    insert();
    pop();
    }




    My questions are :

    1) I have to make this stack implementation, as a library.But how do i modify the code to facilitate my requirement ?... If this was c++, i would have had a class and member functions.


    So if i create an instance of a class STACK ,then i could perform insert(),pop() etc ....as they are the member functions of the class.

    But how do i bring the same concept in pure c. How do i change the structure of the program to make it ass a library so that it could be used optimally .



    2)how to create a library from such .c file and link it to the main .c file.

    googled results landed me on the makefile.But could not understand it properly.Can i have a clear explanation.It would be reallly really helpfull.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    1) You create a header file where you define the struct you want and declare its related functions. Each function needs to take a struct STACK* parameter to know which STACK object it is operating on. Then, just create a .c file where you include your header and write the function implementations.

    2) To use the library you just include the .h file containing the declarations of the functions in your main program (ie .c file). I feel like you don't understand the compilation, linkage and build processes at all. You should read up on them a little bit.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-05-2010, 09:06 AM
  2. Makefile for a library
    By sirmoreno in forum Linux Programming
    Replies: 5
    Last Post: 06-04-2006, 04:52 AM
  3. Replies: 19
    Last Post: 01-12-2006, 11:04 AM
  4. Making Static Library Files
    By fh791 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2004, 07:06 AM
  5. Making Library
    By Anglos in forum C Programming
    Replies: 7
    Last Post: 04-14-2002, 12:38 PM