C/C++ libraries

Its been ages since I did any proper C/C++ so I had to remind myself of a few things, particularly the compilation of libraries. Libraries are really useful as it simplifies multiple use and sharing of components. I’m a Linux person so I’m going to look at this with a Linux hat on. In Linux there are two types; Static libraries (.a) and dynamically linked shared object libraries (.so). Let take a look at static libraries.
Lets create a library, firstly lets define some code, say in a file example1.c:
To create a library you can do:
cc -c example1.c
which compiles the code. Then you need to make the libary:
ar -cvq libexample.a example1.o
(you can of course include other object files here)
Say you call the function in you maincode.c to now compile this do:
cc -o maincode maincode.c libexample.a

Advertisements

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: