function library (from scratch)?

This is a discussion on function library (from scratch)? within the C Programming forums, part of the General Programming Boards category; Hi. I've been programming in C for about 6 months and I'm curious to know how I would go about ...

  1. #1
    Registered User SirJiub's Avatar
    Join Date
    Jun 2011
    Location
    0xfff0
    Posts
    3

    function library (from scratch)?

    Hi. I've been programming in C for about 6 months and I'm curious to know how I would go about making a function library completely from scratch? (without #including anything from the standard library)
    I understand the general concept behind how libraries work...source code is compiled, then functions are linked in from the library (either at runtime or at compilation i think) and then you have an executable.
    I'm fairly new to programming so i don't know if i'm getting myself "too deep" here thinking about this, or if I have to base my library off of another one. I'm guessing that the compiler, OS, and architecture would have to be kept in mind too...I'm using gcc with Debian, and the architecture is x86 based.
    Thanks for help in advance!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The easiest of these to start trying to duplicate will be something like ctype.h. Look and see what functions it has, and then try to make them on your own. The stdio.h and stdlib.h functions are going to be harder than making say string.h. I used to have a book that covered making your own standard library, but I guess I gave it away.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User SirJiub's Avatar
    Join Date
    Jun 2011
    Location
    0xfff0
    Posts
    3
    yeah i get making the headers. they just prototype the functions, and they are defined in a *.c file which is compiled into a library (i think) but what i don't get is how you're going to define those functions to DO anything if you don't already have any functions to work with.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,290
    The first step would be to decide what type of library you want to create.

    The basic question of what you expect the library to achieve is important. If you are seeking to give an alternative to standard library functions, you need to specify what makes yours better - particularly if you want other people to use your library.

    There is actually no such thing as "from scratch". Any library you write will rely on some understanding or assumptions about the target system (eg what services the system provides that you can rely on - even if that is a set of machine registers and assembler instructions) and how functions in the library will be used. If you are doing file I/O, for example, you are relying on some services from the host system.

    There is also the question of portability: if you are not using the standard library, and care about your library being usable across operating systems, compilers, etc then you need to define a workable API (eg a set of function signatures) and also worry about concerns of porting the library to multiple systems. If you are not using the standard library, then the likelihood of your library requiring different implementations for different systems will increase.

    A practical concern - for any library you want someone else to use - is whether you intend to distribute your library as source code or as compiled binaries. If you distribute as source code, you allow users of the library to make changes if they need to (to port it, to enhance it). That means you need to manage the potential of getting bug reports for versions of your library that someone else has changed. If you distribute as binary (compiled form) you need to be clear about what systems and compilers you support, ensure your verification and testing works on all those systems (and is applied), ensure porting to new platforms is handled in a timely manner, etc etc.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User SirJiub's Avatar
    Join Date
    Jun 2011
    Location
    0xfff0
    Posts
    3
    thank you for the reply grumpy. (also forgot quzah earlier! )

    i get what you're saying - portability of source code is always important. however, this was more out of curiosity than anything; i wasn't exactly planning on having anyone else using it.
    and about what you said regarding how "a library from scratch" doesn't exist, that pretty much means that a library that I make would have to be based off of another library?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by SirJiub View Post
    but what i don't get is how you're going to define those functions to DO anything if you don't already have any functions to work with.
    You have to write the functions. I thought that's what you said you wanted to do? There's a reason I suggested starting with ctype instead of say... stdio. It has the easiest class of functions for you to try to make on your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I would write something that re-use standard library rather than re-implementing them first.
    Eg like generic container (list,hash table,tree,etc) or

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by SirJiub View Post
    thank you for the reply grumpy. (also forgot quzah earlier! )

    i get what you're saying - portability of source code is always important. however, this was more out of curiosity than anything; i wasn't exactly planning on having anyone else using it.
    and about what you said regarding how "a library from scratch" doesn't exist, that pretty much means that a library that I make would have to be based off of another library?
    It doesn't necessarily go back to another library--it may go down to system calls (which of course would vary from system to system). For instance, for stdio, you might translate printf() calls to write() calls on *nix, or whatever the equivalent is on DOS. But you're not going to be able to ignore/bypass the OS, really.

    But there are some functions that are just functions -- the ctype functions (as mentioned above several times) don't require any extra anything to make.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. COM from Scratch
    By avagayard in forum C++ Programming
    Replies: 0
    Last Post: 12-11-2010, 09:56 PM
  2. Need to bring this function up to scratch
    By cboard_member in forum C++ Programming
    Replies: 8
    Last Post: 03-11-2006, 02:14 AM
  3. Pong...from scratch
    By blankstare77 in forum Game Programming
    Replies: 17
    Last Post: 07-30-2005, 11:26 PM
  4. custom control from scratch
    By underthesun in forum Windows Programming
    Replies: 8
    Last Post: 01-19-2005, 12:29 AM
  5. scratch and error
    By curlious in forum C++ Programming
    Replies: 12
    Last Post: 07-30-2003, 01:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21