Patterns in static



Instead of giving lengthy compiler commands at the command prompt, you can use a Makefile to do most of the work. How to:

  • Copy and paste the following into a file named makefile.
  • Change the first line to the name of your program (e.g., if you have written census.c, then the first line will read PROGNAME=census).
  • If your program has multiple .c files, add a corresponding .o to the currently blank objects variable, e.g. objects=sample2.o sample3.o
  • One you have a Makefile in the directory, simply type make at the command prompt to generate the executable.
PROGNAME = your_program_name_here
objects =
CFLAGS = -g -Wall -O3
LDLIBS = -lapophenia -lgsl -lgslcblas -lsqlite3
$(PROGNAME): $(objects)
  • If your system has pkg-config, then you can use it for a slightly more robust and readable makefile. Replace the above C and link flags with:
    CFLAGS = -g -Wall `pkg-config --cflags apophenia` -O3
    LDLIBS = `pkg-config --libs apophenia`
    The pkg-config program will then fill in the appropriate directories and libraries. Pkg-config knows Apophenia depends on the GSL and database libraries, so you need only list the most-dependent library.
  • The -O3 flag is optional, asking the compiler to run its highest level of optimization (for speed).
  • GCC users may need the –std=gnu99 or –std=gnu11 flag to use post-1989 C standards.
  • Order matters in the linking list: the files a package depends on should be listed after the package. E.g., since sample.c depends on Apophenia, gcc sample.c -lapophenia will work, while gcc -lapophenia sample.c is likely to give you errors. Similarly, list -lapophenia before -lgsl, which comes before -lgslcblas.