Difference between revisions of "Basic compilation"

From Bio++ Wiki
Jump to: navigation, search
((1) Harmonization of the argument orders in the compilation lines. (2) Remarks section. (3) Modification of the libs' path used.)
(Source files must precede library inclusions ; layout revamping)
 
Line 1: Line 1:
 
[[Category:Usage]]
 
[[Category:Usage]]
This page explains how to compile a program that uses Bio++ (source file <tt>biohello.cpp</tt>) using g++. It is intended at linux users primarily.
+
This page explains how to compile a program that uses Bio++ (source file <tt>biohello.cpp</tt>) using g++.
  
  
In this example we assume that the libraries binaries (<tt>.so</tt> and/or <tt>.a</tt> files) are located in directory <tt>$HOME/.local/lib</tt> and that the library includes tree (<tt>.h</tt> files) are located in directory <tt>$HOME/.local/include</tt>.
+
In this example we assume that the libraries binaries (<tt>.so</tt> and/or <tt>.a</tt> files) are located in directory <tt>$HOME/local/bpp/dev/lib</tt> and that the library includes tree (<tt>.h</tt> files) are located in directory <tt>$HOME/local/bpp/dev/include</tt>.
 
Because these places are non-standard (ie. not <tt>/usr/local/lib</tt>) they must be passed to the compiler through the <tt>-L</tt> and <tt>-I</tt> parameters.
 
Because these places are non-standard (ie. not <tt>/usr/local/lib</tt>) they must be passed to the compiler through the <tt>-L</tt> and <tt>-I</tt> parameters.
  
====== Remarks ======
+
<source lang="bash">
 
+
bpp_dir=$HOME/local/bpp/dev
* The <tt>CPATH</tt>/<tt>CPLUS_INCLUDE_PATH</tt> and <tt>LIBRARY_PATH</tt> environment variables can be used instead of the <tt>-I</tt> and <tt>-L</tt> parameters, respectively.
+
</source>
 
 
* Programs compiled with dynamic linkage will not run without runtime access to the libraries (<tt>.so</tt> files). If the libraries are installed in a non-standard place (ie. not <tt>/usr/local/lib</tt>), this place must be added to the runtime <tt>LD_LIBRARY_PATH</tt> environment variable.
 
  
== Dynamic linkage ==
+
====== Dynamic linkage ======
  
 
<source lang="bash">
 
<source lang="bash">
g++ -I$HOME/.local/include \
+
g++ biohello.cpp \
    -L$HOME/.local/lib \
 
    -lbpp-core -lbpp-seq -lbpp-phyl -lbpp-popgen \
 
 
     -o biohello \
 
     -o biohello \
     biohello.cpp
+
     -I$bpp_dir/include \
 +
    -L$bpp_dir/lib \
 +
    -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core
 
</source>
 
</source>
Here the five libraries are included, you may omit the two last ones if you do not use phylogenetic and/or population genetics classes.
 
  
== Static linkage ==
+
Programs compiled with dynamic linkage will not run without runtime access to the libraries (<tt>.so</tt> files). If the libraries are installed in a non-standard place (ie. not <tt>/usr/local/lib</tt>), this place must be added to the runtime <tt>LD_LIBRARY_PATH</tt> environment variable.
 +
 
 +
====== Static linkage ======
  
When compiling with static linkage, the order of the libraries to include is important:
 
 
<source lang="bash">
 
<source lang="bash">
g++ --static
+
g++ biohello.cpp \
    -I$HOME/.local/include \
 
    -L$HOME/.local/lib \
 
    -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core \
 
 
     -o biohello \
 
     -o biohello \
     biohello.cpp
+
     --static \
 +
    -I$bpp_dir/include \
 +
    -L$bpp_dir/lib \
 +
    -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core
 
strip biohello
 
strip biohello
 
</source>
 
</source>
Here again you may omit the PopGen and Phyl libraries.
 
  
 
Programs compiled with static linkage are stand-alone programs and do not require the libraries to be present when running. The <tt>strip</tt> command removes all unused code from the executable, and efficiently decreases its size.
 
Programs compiled with static linkage are stand-alone programs and do not require the libraries to be present when running. The <tt>strip</tt> command removes all unused code from the executable, and efficiently decreases its size.
 +
 +
====== Remarks ======
 +
 +
* Where on the command line and in which order the libraries are included (-l options) does matter.
 +
 +
* The <tt>CPATH</tt>/<tt>CPLUS_INCLUDE_PATH</tt> and <tt>LIBRARY_PATH</tt> environment variables can be used instead of the <tt>-I</tt> and <tt>-L</tt> parameters, respectively.

Latest revision as of 18:19, 12 November 2012

This page explains how to compile a program that uses Bio++ (source file biohello.cpp) using g++.


In this example we assume that the libraries binaries (.so and/or .a files) are located in directory $HOME/local/bpp/dev/lib and that the library includes tree (.h files) are located in directory $HOME/local/bpp/dev/include. Because these places are non-standard (ie. not /usr/local/lib) they must be passed to the compiler through the -L and -I parameters.

<source lang="bash"> bpp_dir=$HOME/local/bpp/dev </source>

Dynamic linkage

<source lang="bash"> g++ biohello.cpp \

   -o biohello \
   -I$bpp_dir/include \
   -L$bpp_dir/lib \
   -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core

</source>

Programs compiled with dynamic linkage will not run without runtime access to the libraries (.so files). If the libraries are installed in a non-standard place (ie. not /usr/local/lib), this place must be added to the runtime LD_LIBRARY_PATH environment variable.

Static linkage

<source lang="bash"> g++ biohello.cpp \

   -o biohello \
   --static \
   -I$bpp_dir/include \
   -L$bpp_dir/lib \
   -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core

strip biohello </source>

Programs compiled with static linkage are stand-alone programs and do not require the libraries to be present when running. The strip command removes all unused code from the executable, and efficiently decreases its size.

Remarks
  • Where on the command line and in which order the libraries are included (-l options) does matter.
  • The CPATH/CPLUS_INCLUDE_PATH and LIBRARY_PATH environment variables can be used instead of the -I and -L parameters, respectively.