wiki:user-examples

Version 3 (modified by anonymous, 14 years ago) (diff)

--

Linking your Code with COIN Libraries

Again, this information only pertains to UNIX-like systems, where you use configure and make. If you want to use the MS Developer Studio, please check here for more information.

Assume that you have dowloaded the package Pkg in the directory Coin-Pkg. You have run configure, make, and make install from the directory Coin-Pkg. Assume that you used Coin-Pkg as the install directory (i.e. the default), obtaining a library Pkg.so in Coin-Pkg/lib and include files in Coin-Pkg/include.

For most packages, the main directory contains an examples subdirectory. Assuming that this is the case for the package Pkg, the directory Coin-Pkg/examples contains a Makefile that has been adapted to your system. If you want to hook up your own code to this COIN library, it might be a good idea to start by looking at this Makefile.

Note: You should use only the libraries and header files that have been installed in Coin-Pkg/lib and Coin-Pkg/include not those in any other directory. We are using the GNU autotools to compile the code, and picking up libraries and headers from the source directories has a good chance of creating problems.

In order to modify examples/Makefile to compile your own code, you usually only have to change the first part, which might look like this:

##########################################################################
#    You can modify this example makefile to fit for your own program.   #
#    Usually, you only need to change the five CHANGEME entries below.   #
##########################################################################

# CHANGEME: This should be the name of your executable
EXE = my_prog

# CHANGEME: Here is the name of all object files corresponding to the source
#           code that you wrote in order to define the problem statement
OBJS =  my_prog.o \
        additional_source1.o \
        additional_source2.o

# CHANGEME: Additional libraries
ADDLIBS = -L$(HOME)/MyLibrariesDir -ladditinal_lib

# CHANGEME: Additional flags for compilation (e.g., include flags)
ADDINCFLAGS = -I$(HOME)/MyCoolPrograms/include

# CHANGEME: Directory to the sources for the (example) problem definition
# files
SRCDIR = .
VPATH = .

The modification is pretty straightforward:

  • EXE should be set to the name of the executable you want to compile with this Makefile.
  • OBJS should be set to the list of all object files that correspond to the source files you want to compile.
  • ADDLIBS should list additonal compiler link flags to link with (non-COIN) libraries required to link your final program.
  • ADDINCFLAGS should be set to compiler flags that tell it where to look for additional (non-COIN) header files required to compile your code - if required.
  • SRCDIR and VPATH should be set to the directory where your source code is. (This allows you to compile your code with the Makefile in a different directory from the source code directory.)

The lower part of the Makefiles looks somewhat like this:

##########################################################################
#  Usually, you don't have to change anything below.  Note that if you   #
#  change certain compiler options, you might have to recompile Ipopt.   #
##########################################################################

# C++ Compiler command
CXX = g++

# C++ Compiler options
CXXFLAGS = -O3 -fomit-frame-pointer -pipe -DNDEBUG  -pedantic-errors \
      -Wimplicit -Wparentheses -Wreturn-type -Wcast-qual -Wall \
      -Wpointer-arith -Wwrite-strings -Wconversion

# additional C++ Compiler options for linking
CXXLINKFLAGS =  -Wl,--rpath -Wl,/home/me/CoinStuff/lib

# Directory with COIN header files
COININCDIR = /home/me/CoinStuff/include

# Directory with COIN libraries
COINLIBDIR = /home/me/CoinStuff/lib

# Libraries necessary to link with Clp
LIBS = -L$(COINLIBDIR) -lCbc -lCgl -lOsiClp -lOsiCbc -lOsi -lClp -lCoinUtils \
         \
        `cat $(COINLIBDIR)/cgl_addlibs.txt` \
        `cat $(COINLIBDIR)/osi_addlibs.txt` \
        `cat $(COINLIBDIR)/clp_addlibs.txt` \
        `cat $(COINLIBDIR)/coinutils_addlibs.txt`

# Necessary Include dirs (we use the CYGPATH_W variables to allow
# compilation with Windows compilers)
INCL =  -I`$(CYGPATH_W) $(COININCDIR)` $(ADDINCFLAGS)

# The following is necessary under cygwin, if native compilers are used
CYGPATH_W = echo

all: $(EXE)

.SUFFIXES: .cpp .c .o .obj

$(EXE): $(OBJS)
        bla=;\
        for file in $(OBJS); do bla="$$bla `$(CYGPATH_W) $$file`"; done; \
        $(CXX) $(CXXLINKFLAGS) $(CXXFLAGS) -o $@ $$bla $(ADDLIBS) $(LIBS)

clean:
        rm -rf $(EXE) $(OBJS)

.cpp.o:
        $(CXX) $(CXXFLAGS) $(INCL) -c -o $@ `test -f '$<' || echo '$(SRCDIR)/'`$<


.cpp.obj:
        $(CXX) $(CXXFLAGS) $(INCL) -c -o $@ `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(SRCDIR)/$<'; fi`

.c.o:
        $(CC) $(CFLAGS) $(INCL) -c -o $@ `test -f '$<' || echo '$(SRCDIR)/'`$<


.c.obj:
        $(CC) $(CFLAGS) $(INCL) -c -o $@ `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(SRCDIR)/$<'; fi`

The compiler name variables CXX}}, {{{CC and F77 will already be adapted to your system, as well as the corresponding variables for the compiler flags. Those are the same that have been used to compile the COIN library, and it is probably not a bad idea to choose similar (or compatible) options.

The COININCDIR and COINLIBDIR will have been adapted to the directory where the COIN libraries have been installed. (In the above example, it is assumed that you ran configure with the flag --prefix=/home/me/CoinStuff, so that the result of the COIN compilation was installed into subdirectories there.)

If you compiled the COIN library as a shared object, the CXXLINKFLAGS is set to the compiler link flag that hardcodes the search path to the installed library into the executables (if that is supported on your system). Alternatively, you can set the LD_LIBRARY_PATH or similarly to include the directory with the shared object.

The LIBS variable is set to the compiler link flags that are required to link with the COIN library. In the above example, it is the Cbc library. Note the `cat $(COINLIBDIR)/*_addlibs.txt` entries --- when a COIN library might have additional library dependency (e.g., for the GNU package zlib), the corresponding compiler link flags are listed in the *_addlibs.txt file as a string. These files are installed in the same location as the libraries.

The generic compilation rules (e.g., .c.o) might look somewhat complicated. But with this, the Makefile can also be used by people how work with the MS compilers (e.g., cl) under Cygwin, since those compilers don't understand the UNIX-type directory paths. In such a case, the CYGPATH_W will have been set to cygpath -w by configure.