
= Use installed libusual =


Install libusual and link against it.


== Build libusual ==

---------------------------------
$ git clone git://github.com/libusual/libusual.git libusual
Cloning into 'libusual'...
$ cd libusual
$ ./autogen.sh
[...]
$ ./configure --disable-static --prefix=`pwd`/../inst
[...]
$ make
[...]
$ make install
[...]
$ cd ..
$ find inst | sort
inst
inst/include
inst/include/usual
inst/include/usual/aatree.h
inst/include/usual/base.h
inst/include/usual/base_win32.h
inst/include/usual/bits.h
inst/include/usual/cbtree.h
inst/include/usual/cfparser.h
inst/include/usual/config.h
inst/include/usual/config_msvc.h
inst/include/usual/crypto
inst/include/usual/crypto/chacha.h
inst/include/usual/crypto/csrandom.h
inst/include/usual/crypto/digest.h
inst/include/usual/crypto/entropy.h
inst/include/usual/crypto/hmac.h
inst/include/usual/crypto/keccak.h
inst/include/usual/crypto/keccak_prng.h
inst/include/usual/crypto/md5.h
inst/include/usual/crypto/sha1.h
inst/include/usual/crypto/sha256.h
inst/include/usual/crypto/sha3.h
inst/include/usual/crypto/sha512.h
inst/include/usual/ctype.h
inst/include/usual/cxalloc.h
inst/include/usual/cxextra.h
inst/include/usual/daemon.h
inst/include/usual/endian.h
inst/include/usual/err.h
inst/include/usual/fileutil.h
inst/include/usual/fnmatch.h
inst/include/usual/getopt.h
inst/include/usual/hashing
inst/include/usual/hashing/crc32.h
inst/include/usual/hashing/lookup3.h
inst/include/usual/hashing/memhash.h
inst/include/usual/hashing/siphash.h
inst/include/usual/hashing/spooky.h
inst/include/usual/hashing/xxhash.h
inst/include/usual/hashtab-impl.h
inst/include/usual/heap.h
inst/include/usual/json.h
inst/include/usual/list.h
inst/include/usual/logging.h
inst/include/usual/mbuf.h
inst/include/usual/mdict.h
inst/include/usual/mempool.h
inst/include/usual/misc.h
inst/include/usual/netdb.h
inst/include/usual/pgutil.h
inst/include/usual/psrandom.h
inst/include/usual/pthread.h
inst/include/usual/regex.h
inst/include/usual/safeio.h
inst/include/usual/shlist.h
inst/include/usual/signal.h
inst/include/usual/slab.h
inst/include/usual/socket.h
inst/include/usual/socket_win32.h
inst/include/usual/statlist.h
inst/include/usual/string.h
inst/include/usual/strpool.h
inst/include/usual/talloc.h
inst/include/usual/time.h
inst/include/usual/tls
inst/include/usual/tls/tls.h
inst/include/usual/tls/tls_cert.h
inst/include/usual/utf8.h
inst/include/usual/wchar.h
inst/lib
inst/lib/libusual.la
inst/lib/libusual.so
inst/lib/libusual.so.0
inst/lib/libusual.so.0.0.0
inst/lib/pkgconfig
inst/lib/pkgconfig/libusual.pc
inst/share
inst/share/aclocal
inst/share/aclocal/antimake.m4
inst/share/aclocal/usual.m4
inst/share/libusual
inst/share/libusual/amext-cxx.mk
inst/share/libusual/amext-libusual.mk
inst/share/libusual/amext-modes.mk
inst/share/libusual/amext-msvc.mk
inst/share/libusual/antimake.mk
inst/share/libusual/find_modules.sh
inst/share/libusual/std-autogen.sh
---------------------------------

== Build our own code ==


Now we prepare our own code.


First, this is the source file:

.File: prog.c
[source,c]
-----------------------------------
#include <stdio.h>
#include <string.h>
#include <usual/hashing/crc32.h>

int main(void)
{
	const char *data = "CECSFXX";
	uint32_t crc;

	crc = calc_crc32(data, strlen(data), 0);
	printf("crc: %08x\n", crc);
	return 0;
}
-----------------------------------

Here is corresponding Makefile:

.File: Makefile
[source,makefile]
-----------------------------------

CC = gcc
CFLAGS = -O -g -Wall

# here we describe our program
SRCS = prog.c
OBJS = $(SRCS:.c=.o)

# put libusual flags to proper place
CPPFLAGS = $(USUAL_CPPFLAGS)
LIBS = $(USUAL_LIBS)

# use pkg-config to get libusual info
USUAL_CPPFLAGS = $(shell $(PKG_CONFIG) --cflags libusual)
USUAL_LIBS = $(shell $(PKG_CONFIG) --libs libusual)

# temo hacks to support local install, not needed otherwise
PKG_CONFIG := PKG_CONFIG_PATH=$(CURDIR)/inst/lib/pkgconfig pkg-config
CPPFLAGS := $(subst $(CURDIR)/libusual/../,./,$(CPPFLAGS))
LIBS := $(subst $(CURDIR)/libusual/../,./,$(LIBS))

all: prog

%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

prog: $(OBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) $(LIBS) -o $@
-----------------------------------

Build the project

---------------------------------
$ make
gcc -O -g -Wall -I./inst/include   -c -o prog.o prog.c
gcc -O -g -Wall  prog.o -L./inst/lib -lusual   -o prog
$ ls
Makefile  inst	libusual  prog	prog.c	prog.o
$ LD_LIBRARY_PATH=./inst/lib ./prog
crc: 12345678
---------------------------------

Done!

