Example of building and using an external library with SafeStr

Installation

Download SafeStr from the homepage http://www.zork.org/safestr/

First compile and install SafeStr. Below, we install SafeStr into a separate directory with the --prefix option, but that's not necessary if you have administrator rights.

  $ ./configure --prefix=$HOME/opt/safestr-1.0.3
  $ make && make install

If you used the --prefix option, set the LD_LIBRARY_PATH as follows:

  $ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/opt/safestr-1.0.3/lib

This is necessary, because with --prefix you don't install the libraries in a standard directory like /lib or /usr/lib. When the program is run, the system must be able to find the libsafestr.so and libxxl.so files. Hence, it looks up all directories in the LD_LIBRARY_PATH environment variable.

Testing

First, we create a simple test file:

  #include <stdio.h>
  #include "safestr.h"
  
  int main(void)
  {
      safestr_t str;
      str = safestr_create("Hello, world!", 0);
      printf("%s\n", str);
      return 0;
  }

The Makefile looks like this (and don't forget tabs):

  CFLAGS += -g3 -I$(HOME)/opt/safestr-1.0.3/include
  LDFLAGS += -L$(HOME)/opt/safestr-1.0.3/lib -lsafestr -lpthread
  OBJS = testsafestr.o
  
  %.o : %.c
  	$(CC) -c $(CFLAGS) $< -o $@
  
  all: testsafestr
  
  testsafestr: $(OBJS)
  	$(CC) $(LDFLAGS) $(OBJS) -o $@
  
  clean:
  	rm -f *.o testsafestr

Compile the program with the makefile and run the result:

  $ make
  cc -c -g3 -I/home/bartvk/opt/safestr-1.0.3/include testsafestr.c -o testsafestr.o
  cc -L/home/bartvk/opt/safestr-1.0.3/lib -lsafestr -lpthread testsafestr.o -o testsafestr
  $ ./testsafestr
  Hello, world!
  $