Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts

Sunday, February 08, 2009

Dynamic Load Library - Linux

Have you ever wondered, how to load, link and execute library functions when it is needed and unload it after it is used in Linux.

libdl - Library gives you this opportunity. For example:

* If you are writing an application which makes use of XML Parsing Library sparingly. You may want to load this XML Library when there is need and unload it afterwards.
* Or, you may want to load choose a particular library depending on the runtime parameters of an application.

libdl comes in handy, with only 3 interfaces with in the library. The following functions are defined in "dlfcn.h".

* dlopen - gain access to an executable object file
* dlsym - obtain the address of a symbol from a dlopen object
* dlclose - close a dlopen object
* dlerror - dlerror - get diagnostic information

void *handle;
int *iptr, (*fptr)(int);

/* open the needed object */
handle = dlopen("/usr/home/me/libfoo.so", RTLD_LOCAL | RTLD_LAZY);

/* find the address of function and data objects */
*(void **)(&fptr) = dlsym(handle, "my_function");
iptr = (int *)dlsym(handle, "my_object");

/* invoke function, passing value of integer as a parameter */
(*fptr)(*iptr);



Don't forget to set the LD_LIBRARY_PATH.

Wednesday, June 25, 2008

Pointer to functions - Simplified

Here is the code


1 #include < iostream >
2
3 using namespace std;
4
5 int add(int a, int b) {
6 return a+b;
7 }
8
9 int sub(int a, int b) {
10 return a-b;
11 }
12
13 // typdef pointer to a function
14 typedef int (*fpp)(int, int);
15
16 // Array of pointers to function
17 fpp fp[2];
18
19 // Function returning a pointer to a function
20 int (* getfp(int i) ) (int, int) {
21 if(i==1) return & add;
22 else return & sub;
23 }
24
25 // Pointer to a function returning a pointer to a function
26 int (* (*fgetfp)(int)) (int, int);
27
28 int main() {
29
30 fgetfp = &getfp;
31
32 fp[0] = (*fgetfp)(1); // same as fgetfp(x)
33 cout<<"10 + 20 = "<< fp[0](10,20)<< endl;
34
35 fp[1] = fgetfp(2);
36 cout<<"10 - 20 = "<< fp[1](10,20)<< endl;
37 return 0;
38 }
39

Wednesday, May 14, 2008

Make "make" work for you

Make - tool to control the compilation and creation of executables. I was hacking into this tool for quite sometime. I feel "make" is good for all the linux/unix c/c++ programmers.

Here is a simple Tutorial of Make:

source file (helloworld.c)
========
#include ...
int main() {
using namespace std;

cout<<"hello world\n"; return 0;
}
simple makefile(gnumakefile/makefile)
===========
helloworld: helloworld.c
TAB g++ -o helloworld helloworld.c

TAB - is important, otherwise gnumake will prompt for error.

command
=======
make TARGET

make helloworld

Example 2
========

/* sum.c */

int sum(int a, int b) {
return a+b;
}

/* diff.c */

int diff(int a, int b) {
return a-b;
}

/* main.c */

int main() {
...
c=sum(20,30);
...
d=diff(30,23);
...
}

/* Make file */

CC = g++ /* which compiler*/
CFLAGS = -Wall -g -ansi /* compiler flags */

sum.o: sum.c
$(CC) $(CFLAGS) -c -o $@ $<
diff.o: diff.c
$(CC) $(CFLAGS) -c -o $@ $<
main: sum.o diff.o main.c
$(CC) $(CFLAGS) -o $@ $^
clean:
-rm core *.o main
Special Tags
=========
$@ -- current target name
$LT -- First name of the prerequisite
$^ -- List of all prerequisite
$* -- List of all prerequisite (without prefix)

So, dudes, start using make. It is cool, it makes life a lot easier for c/c++ programmers.

Friday, February 22, 2008

Coding Books

Books About Coding
===================

* Beautiful Code
* The Best Software Writing
* Programmers at Work
* Out of their Minds
* Code Complete
* Zen of Assembly Language (Michael Abrash)