Compiling and Linking with Installed Libraries

In this example, we'll link with an existing library in $G called ggargs. It is a package that can be used to parse command line arguments. The source for ggargs is in $G/src/ggargs. The library archive files (in debug, optimized, and profiling versions) are installed in $G/lib and include files are in $G/include/ggargs. So, we'll need to setup a Makefile that links with the appropriate library file and puts $G/include/ggargs in the include path.

Here is an example program file (main.cpp) that uses the library:

#include <args.H>
#include <stdio.h>

static char *inputFileName  = "in.data";
static char *outputFileName = "out.data";
static int   dim = 1;

args_spec args[] = {
  {A_HELPMSG, "Takes input data and converts it to output data." },
  {"d", "Specifies the dimension of that data", {{A_INT, A_PTR &dim}}},
  {A_NODASH, "Input and output files",
    {{A_CHARSTAR, A_PTR &inputFileName}, {A_CHARSTAR, A_PTR &outputFileName}}, A_REQUIRED},
  { A_END }
};


int main(int argc, char *argv[]) {
  args_scanx(args, argc, argv);

  printf("Data dimension = %d\n", dim);
  printf("Input file is: %s\n", inputFileName);
  printf("Output file is: %s\n", outputFileName);

  return 0;
}

And here is the Makefile that sets up the link with ggargs:

PROJECT_NAME = mytest
OBJDIR = obj

SRC = main.cpp

# Space separated list of subdirectories of $G/include to add to the
# include path.  
G_INCLUDE_DIRS = ggargs

# Space separated lists of libraries to link in the debug, optimized,
# and profiling cases.  Leave the lib and the .a in libggargs.a off
# this line, likewise, if you are working in Windows, leave the .lib
# off the end of the library name.  These will be added automatically
# by Make.gfxtools.rules.  If we wanted to always link with the
# optimized version of ggargs, then we could use a different line:
# LIBS = ggargs 
# Libraries specified on the LIBS line are linked in all cases.

DEBUG_LIBS =  ggargs-d
OPT_LIBS   =  ggargs
PROF_LIBS  =  ggargs-p


all:   progs
debug: progg
opt:   progo 
prof:  progp 

include $(G)/lib/Make.gfxtools.rules
-include Makefile.depend

More advanced issues

Including headers outside of $G: The Makefile variable

INCLUDE_DIRS=my_dir /usr/local/include/blah
can be specified to add directories that are not inside $G/include to the include path. In this example, we would look for a directory called "my_dir" inside the current working directory and another directory "/usr/local/include/blah" that is specified with a full path.

Linking with libraries in subdirectories of $G/lib: Sometimes libraries are installed in subdirectories of $G/lib. Let's say you want to link with $G/lib/gluebase/libgluebase.a The directory $G/lib/gluebase needs to be included in the library search path. To do this, we specifiy it on the G_LIB_DIRS line in Makefile.

G_LIB_DIRS=gluebase

Linking with libraries outside of $G: Add directories to search for additional libraries to the "LIB_DIRS" line in the Makefile. For example to include the X11 libraries in your search path in Linux you could add:

LIB_DIRS=/usr/X11R6/lib

Linking with shared (dynamically loaded) libaries: Directories for shared libraries are specified in the same way as described above. The library names are specified with the following Makefile variables.

# Shared libraries to link in all cases
SHLIBS=
# Shared libraries to link in debug, optimitzed, and profiling cases
DEBUG_SHLIBS=
OPT_SHLIBS=
PROF_SHLIBS=