C programming…

Search path for include and library files

http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

how to specify additional search paths:

http://www.network-theory.co.uk/docs/gccintro/gccintro_21.html

The following environment variables are useful: http://stackoverflow.com/questions/558803/how-to-add-a-default-include-path-for-gcc-in-linux

How to initialize a big array

http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c

int array[1024] = {[0 ... 1023] = 5};

How to use dynamic memory management

http://www.cppreference.com/wiki/memory/c/start

How to compile and use a library

http://www.adp-gmbh.ch/cpp/gcc/create_lib.html

http://www.faqs.org/docs/Linux-HOWTO/Program-Library-HOWTO.html#INIT-AND-CLEANUP

I compile the library using

gcc -c test.c

Then I link

ar rvs test.a test.o

and finally

gcc main.c test.a -lm -lgsl -lgslcblas -o main

If it is a dynamic library:

gcc  -fPIC -c test.c -o test.o

ld -shared -o libtest.so test.o

However, I made the experience that in this case the constructor and destructor of the library are not executed during loading. Therefore one should use

gcc -fPIC testlib.c -shared  -Wl,-soname,libtest.so -o libtest.so

instead. In this works. Constructor and destructors are written like this

static void __attribute__ ((constructor)) lib_init(void);
static void __attribute__ ((destructor))  lib_clean(void);

Using the library:

gcc main.c -o main -L. -ltest -lm -ldl -lgsl -lgslcblas

Here -o specifies the name of the executable file, -L the library path, -ltest a library called libtest.so and dl are for dynamical library loading, m for math, gsl and gslcblas for GSL support.

Before running the program one has to set the environment variable like this

LD_LIBRARY_PATH=.

Alternatively we can use the .bashrc script

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/somepath
export LD_LIBRARY_PATH

Error undefined reference to `vtable for xxx

Check the order of libraries  http://stackoverflow.com/questions/1095298/gcc-c-linker-errors-undefined-reference-to-vtable-for-xxx-undefined-referen

Library which uses another library

The option

-Wl,-export-dynamic

has to be used sometimes in order that undefined symbols are handled appropriate, see http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html.

How to initialize a library

Also how library constructor/destructor work.

http://stackoverflow.com/questions/1681145/how-to-initialize-a-shared-library-on-linux

http://my.opera.com/wangyun/blog/show.dml/421806

http://benyossef.com/using-a-shared-library-constructor/

Library information

print symbol information: nm

http://linux.about.com/library/cmd/blcmdl1_nm.htm

object information: objdump

http://sourceware.org/binutils/docs/binutils/objdump.html

To check on which libraries one library depends: ldd http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

Library Howto

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

GCC Howto

http://linux.die.net/man/1/gcc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: