![]() |
||
Learn creating library files in C and C++ |
||
Naturally we do not want to have to include the source file for every
new program that we write using the functions. We should be able to just
include the header file and link it to our library for the precompiled
function. Here in this example, I will create a library that will
contain a function, used to calculate the factorial of an entered
number. I have used Borland C++ Compiler 5.5 (command line) for
compiling my files. You must ensure that you have set the system PATH to
Borland's Bin directory. If you haven't done so already, write down the
following line in the file C:\autoexec.bat (assuming that you have
installed the compiler in C:\Borland directory). SET PATH=%PATH%;C:\Borland\BCC55\Bin Now we come to the steps for creating the header files and source files. Here are the source code for both kind of files, used to build this library. The header file contains the prototype of the function, to be included in the library. Below this para, the contents of fact.h (header file) i.e. a single line, are shown, int factorial(int); Similarly following are the contents of fact.c (source file): int factorial(int num) { if (num == 1) return(1); else return(num * factorial(num - 1)); } I have used recursion for calculating the factorial of a number, you can use any other approach/logic that you have. Before we start the job, let's agree on some specifics here,
Parent directory where the samples are stored is, "C:\Windows\Desktop\mydemos". | | +---INCLUDE | fact.h | +---LIB | | |---SOURCE fact.c Using the code and the directory tree established, from the parent directory i.e. mydemos (where we plan to store the demo programs at) key in the following line to compile the fact.c source file. bcc32 -c source\fact.c This will compile the fact.c file only to OBJ, not try to make an EXE (-c switch). You can remove the -c switch also, if you are not comfortable with it. Since your .c file doesn't contain main() function, no .exe file will be generated. The only output is a .obj file. Once this process is finished, our directory tree should look similar to the following: Parent directory where the samples are stored is, "C:\Windows\Desktop\mydemos". | | | fact.obj | +---INCLUDE | fact.h | +---LIB | | |---SOURCE fact.c Move the file fact.obj to the LIB subdirectory. From DOS use: move fact.obj lib Then change to the LIB subdirectory and create the library called mylib.lib with the following command line. tlib mylib.lib +fact.obj Now the directory tree should look like this: Parent directory where the samples are stored is, "C:\Windows\Desktop\mydemos". | | | +---INCLUDE | fact.h | +---LIB | fact.obj | mylib.lib | |---SOURCE fact.c Now whenever you want to include the factorial function, you can do it much more efficiently. Let's create a sample program using the library to see how it's done. /* calculation.c * * This file does a demo of the factorial function we've written so far. */ #include <conio.h> #include <stdlib.h> #include <stdio.h> /* Enable this line for compile within the IDE */ /* #include "source\fact.c" */ /* Enable this line for compile with at the command line */ #include "include\fact.h" int main(void) { int n = 5; printf("The factorial of 5 is : %d", factorial(n)); return 1; }Save the above to a file called calculation.c in the parent directory. Your directory tree should look like this: Parent directory where the samples are stored is, "C:\Windows\Desktop\mydemos". | | | calculation.c | +---INCLUDE | fact.h | +---LIB | fact.obj | mylib.lib | |---SOURCE fact.c Then at the command prompt, in the parent directory, compile the program to EXE using this command line... bcc32 calculation.c lib\mylib.lib Your directory tree should now look like this: Parent directory where the samples are stored is, "C:\Windows\Desktop\mydemos". | | | calculation.c | calculation.obj | calculation.exe | calculation.tds | +---INCLUDE | fact.h | +---LIB | fact.obj | mylib.lib | |---SOURCE fact.c Now you can run the resulting calculation.exe file to see the results. Above excerpt has been taken from GaryNeal's Web and then modified. You can reach him at geocities. |
||
All rights reserved. Copyright © 1999 - . Krishna Kumar Khatri. Best viewed with Microsoft IE 6 or later, under 800x600 resolution with True Color (24-bit). |