+ Reply to Thread
Results 1 to 4 of 4

Thread: How to make and use a library

  1. #1
    twitch is offline Newbie
    Join Date
    Jun 2007
    Posts
    10
    Rep Power
    0

    Smile How to make and use a library

    I'm going to show you how to make and use a library in C.

    Library's are no different than regular source files.All they do is break the code up into smaller parts that can be maintained better and easier to read.

    It is like your main source file except it has no main function because your program isn't going to start here.

    yourlib.c
    Code:
    /* You must always include your own library.*/
    #include<stdio.h>
    #include"yourlib.h"
    
    void celconvert(float Cel)
    {
    
        float Fah;
    
        Fah = Cel * 9/5 + 32;
        printf("%f",Fah);
    }
    Now you have to create a header file that prototypes all you functions that you want to use in other source files.
    yourlib.h
    Code:
    /*You have to put extern before your prototyped function you want to use in your library*/
    extern void celconvert(float Cel);
    Next you make your main source.
    You need to include your new library to use it.

    main.c
    Code:
    #include <stdio.h>
    #include"yourlib.h"
    
    int main()
    {
    	float UserInput;
    
    	printf("Enter a temperature in celsius to convert to  fahrenheit:");
    	scanf("%f",&UserInput);
    	celconvert(UserInput);
    
    	return 0;
    }
    Now to compile you program there are three ways to do it:

    If you have a IDE it will handle all the compiling for you.

    If you compile from the command line then you have to do it all yourself.

    Or use make.

    For the tutorial I'll teach you how to do it yourself on the command line but for big projects I strongly suggest you learn how to use make because doing it yourself would take a very long time.

    first get to were you have all the source code

    The use your you compiler

    For this tutorial I'm using MinGW which is a version of GCC If your compiler is different please read the documents that came with it on compiling your code.

    First compile you library

    gcc -c yourlib.c

    Then compile your main

    gcc -c main.c

    And to make your program

    gcc -o Yourprogram main.o yourlib.o
    Last edited by twitch; 06-14-2007 at 12:44 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    desh is offline Newbie
    Join Date
    Jun 2011
    Posts
    1
    Rep Power
    0

    Re: How to make and use a library

    which type of library is this?

    is this a dynamic library formation example.
    rasp.
    desh

  4. #3
    Join Date
    Jun 2010
    Location
    Vancouver, Eh.
    Posts
    4,020
    Blog Entries
    7
    Rep Power
    39

    Re: How to make and use a library

    It is an object file of which can be linked with other objects in to an executable, it is not a library in the .dll or .so or .lib sense although can become one.
    Be sure to read the updated FAQ || Health is achieved through 10,000 different steps.
    A textual description can be only part of your question, be sure to provide sample results, errors and your platform in the appropriate forums while asking.

  5. #4
    samualrich123 is offline Newbie
    Join Date
    Jun 2011
    Posts
    5
    Rep Power
    0

    Re: How to make and use a library

    Thanks for sharing this information. It will ery helpful while programming .We can put all function in one library and afterwards we can use it all.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 4 users browsing this thread. (0 members and 4 guests)

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2011, 05:42 PM
  2. Your/my own library
    By Flying Dutchman in forum C and C++
    Replies: 11
    Last Post: 08-12-2010, 08:45 AM
  3. Make own Library file for Visual Studio Languages
    By UbuntuX in forum Visual Basic Programming
    Replies: 7
    Last Post: 08-08-2010, 10:08 AM
  4. Replies: 3
    Last Post: 06-06-2010, 01:26 PM
  5. OCR library
    By webzpoint in forum C# Programming
    Replies: 1
    Last Post: 07-21-2008, 01:11 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts