Thread: How to create a library

  1. #1

    How to create a library

    Hi there,
    I'm a newbie in C and I made some functions, but now I wanna use them as libraries.
    I create a function corr() in a file corr.c and corr.h and compile that with:
    gcc -c corr.c -o corr.o. Now everytime I need to use my function on a program I have to write include the line #include <corr.h> and then type

    gcc corr.o myprog.c -o myprog

    I want to know if there is any way that I needed to include the line #include <corr.h> in my program and I only had to type:

    gcc myprog.c -o myprog

    in order to compile?
    I apreciate if anyone could send me a link where I can find the info I need or if anyone can explain how do I do that.

    Thanks for any help!

  2. #2
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    even when you have the library with you you can not do
    gcc myprog.c -o myprog

    to generate myprog binary you will have to link it with your library.

    gcc -o myprog -L/usr/lib/yourlibrary -I/yourincludepath myprog.c


  3. #3
    Registered User
    Join Date
    Nov 2001

    make library such as stdio.h

    I have the same question.
    How do I made my own library, so I could use it like:

    #include <stdio.h>
    #include <mylib.h>

    without specifying the library path (you don't have to specify
    the path for 'stdio.h', do you)?

  4. #4
    Registered User
    Join Date
    Aug 2001
    computers/ theatre, travelingstudentL>- Northern California NativeNorthern California3D&Tie-DyeStudent1>. Admirer of C Bangalore Net surfingTeacher >/ >0 nagpurteaching >1  >2
    This is the syntax for IRIX 6.5. It should be similair for your version of linux.

    To compile a library:
    cc -shared corr.c -o

    This will create a shared object. you will need to place the file in a directory specified in your LD_LIBRARY_PATH environment variable.

    To compile the executable:
    cc myprog.c -lcorr -o myprog

    The option -lxxx links in objects from the or libxxx.a (in that order) from directories in the LD_LIBRARY_PATH

    This is just like when you use math.h, you have to include the -lm option when you compile.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming library with built in manager
    By cyreon in forum C Programming
    Replies: 5
    Last Post: 06-26-2009, 12:49 PM
  2. What's an import library?
    By chiefmonkey in forum C++ Programming
    Replies: 1
    Last Post: 06-19-2009, 05:00 PM
  3. standrad library
    By sarahr202 in forum C++ Programming
    Replies: 11
    Last Post: 05-18-2009, 08:50 PM
  4. create a static library from C++ and C code
    By lehe in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2009, 07:28 PM
  5. Create a user library
    By mabuhay in forum C Programming
    Replies: 8
    Last Post: 11-03-2005, 03:42 PM