Thread: how do I write a library?

  1. #1
    Registered User
    Join Date
    Oct 2008

    how do I write a library?

    can someone point me to some links so i can learn?

    I tried googling and it keeps pointing to makefile. Is this the same thing?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    What do you currently know about making a library?

    Also, what OS/compiler are you using?

    Here is one place where the process is described:

    A library is essentially a packaged set of files that the linker can extract one or several of into your executable, and they are held together with a "library tool" - ar in Linux, LIB in Windows.

    Once you have a set of source files you want to make into a library, you compile them with "-c" to make it an object file. Then you use your "library tool" to build a library file.

    Other applications use the same library file to link to.

    You also need one or more header files that the application using the library can include, so that the compiler understands what the functions are that come from that library.

    If you need more help, then you need to tell us more about what steps you have tried, what you understand and what you don't understand.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    A library is just a collection of code. A Makefile is an input file for a build automation tool called make. make can automatically compile only those source files which have been modified. It is a great time-saver if you're working with lots of source files.

    For more information about make, see for example

    If you have a collection of code that you'd like to make into a library, well, you can do it. Exactly how depends on your system. If you're using Linux, for example, you might compile all of the source files and put their .o files into one .a archive with the ar command.

    A tutorial about this is available if you google for the right things. I tried "create c library tutorial" and found this.

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2008
    so basically libraries are like functions contained in seperate files right?
    I use Os X mostly, but program in linux for my course.

    dammit, was googling the wrong words (more so combination of words).

  5. #5
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    I have a question on what you said here. So I have a header file in which I declare some global structs and variables and also have about 10 funtions - their declaration only. That means 10 c files as well with the code of each function (BTW Is this bad?) These are all helper function which work in a lower level. I make them a library -libmylib.a - and I also have another file - mainProgram.c - in which I wrote a program where it accepts commands and uses the functions and variables mentioned above to complete them. So if I want to compile the program I have to compile the latter file with the library likeso gcc -o mymainprogram mainProgram.c -L. -lmylib and I have to also have the header in the smae folder - since it uses the global structs,var and funtions declared in it? The c files which implement the 10 function no longer need to be there right?

    I am sorry if I sound confusing but I am. I am not really sure I get what a library does. I know it is an archive of object code but does that mean that any program can use it without the c files which correspond to the header BUT with the header?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    You need the header file so the compiler can see the definitions and the .lib file 'cause that's got the code, and that's it.

    You've been using libraries the whole time -- you haven't written any code for printf, have you? Or included any .c files with it either?

  7. #7
    Registered User Phoenix_Rebirth's Avatar
    Join Date
    Dec 2005
    Well that what I thought but you see I had some code from some guy and I got confused because he had written all the code in two c files and the he included in one of the c files the other c file and I know thats wrong but I got confused and thought better safe than sorry so I asked here :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difficulty choosing graphics library
    By jdiperla in forum Game Programming
    Replies: 11
    Last Post: 02-27-2008, 06:35 PM
  2. How to write documenation for a Library ?
    By jabka in forum C Programming
    Replies: 2
    Last Post: 01-07-2008, 01:01 PM
  3. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  4. Which Sockets Library
    By ChadJohnson in forum Networking/Device Communication
    Replies: 8
    Last Post: 09-15-2004, 05:39 PM
  5. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 07:53 PM