Anders Branderud
Anders Branderud

Reputation: 1918

How to create a C-library (for Ubuntu) out of header- and source-files?

I am looking for a program to create a C-library (i.e. to link and compile the files into one file) based on .h-files and .c-files that have the following structure (it is a FEC-library: www.openfec.org ). I am using Ubuntu. I want it to do this without manually specifying each files. I tried WAF, but got the error 'ERROR:root: error: No module named cflags'.

Here is (part of the) the structure:

fec
 lib_advanced
 ldpc_from_file
   of_code_profile.h
   of_ldpc_ff.h
   ...
 lib_common
   linear_binary_codes_utils
       binary_matrix
       it_decoding
       ml_decoding    

   statistics
   of_cb.h
   of_debug.h
   of_mem.c
   of_mem.h
   of_openfec_api.c
   of_openfec_api.h
   of_openfec_profile.h
   of_types.h       

Thanks!!

Upvotes: 1

Views: 1612

Answers (2)

RedComet
RedComet

Reputation: 1202

C libraries on *nix systems (including all linux distros) are created with standards tools, this tools being a) a C compiler b) a linker b) the ar utility c) the ranlib utility.

The C compiler 99.9% of the time the GNU C compiler, while the linker ld, and the utilities ar and ranlib are part of the binutils package on gnu systems (99.9% of linux systems).

ar and ranlib are used to created static libraries, putting already compiled object files ( *.o files) in an archive file libsomething.a with ar and indexing the archive with ranlib.

The linker can be called inside the gcc compiler to create dynamic libraries with position independent code, again the already compiled files are archived on a special, this file has the .so extension for shared object.

Static Libraries are used for speed and self-containment, they produce big executables which contain all their dependencies inside the final executable. If a single library of many changes, to update it you'll have to recompiled everything.

Dynamic libraries are compiled and linked separately of the executables, they can be used simultaneously by multiple executables, if one library is updated, you just need to recompile a single library and not every executable which depends on it.

The use of this tools are universal and standard procedure, they can vary by few details from *nix systems to *nix system, but on linux you essentially always use the GCC and Binutils packages. Extra build utilities on the form of make, cmake, autotools, etc exist to help on the process, but the basic tools are always used.

Generally on the most basic level you write a Makefile script which is interpreted by the make utility. And depending on your commands it can make one or both kinds of libraries, install the library and executables, uninstall them, clean up, etc

For more information :

http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html
http://www.dwheeler.com/program-library/Program-Library-HOWTO/

Upvotes: 2

Eregrith
Eregrith

Reputation: 4366

You have to use gcc to compile the C-files to objects files.

Then you have to use ar r and then ranlib to pack the objects into one .a library file.

Upvotes: 2

Related Questions