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.)
Line 3: Line 3:
  
  
In this example we assume that the libraries binaries (<tt>.so</tt> or <tt>.a</tt> files) are located in directory <tt>/home/vador/local/lib</tt> and that the library includes tree (<tt>.h</tt> files) are located in directory <tt>/home/vador/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/lib</tt> and that the library includes tree (<tt>.h</tt> files) are located in directory <tt>$HOME/.local/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 ======
 +
 +
* 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.
 +
 +
* 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/vador/local/include \
+
g++ -I$HOME/.local/include \
     -L/home/vador/local/lib \
+
     -L$HOME/.local/lib \
     -lbpp-core -lbpp-seq -lbpp-phyl -lbpp-popgen biohello.cpp \
+
     -lbpp-core -lbpp-seq -lbpp-phyl -lbpp-popgen \
     -o biohello
+
     -o biohello \
 +
    biohello.cpp
 
</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.
 
Here the five libraries are included, you may omit the two last ones if you do not use phylogenetic and/or population genetics classes.
 
Programs compiled with dynamic linkage will not run without runtime access to the libraries (<tt>.so</tt> files). The directories in which libraries are, if non-standard (ie. not <tt>/usr/local/lib</tt>), must be added to the runtime <tt>LD_LIBRARY_PATH</tt> environment variable.
 
  
 
== Static linkage ==
 
== Static linkage ==
Line 23: Line 28:
 
<source lang="bash">
 
<source lang="bash">
 
g++ --static
 
g++ --static
     -I/home/vador/local/include \
+
     -I$HOME/.local/include \
     -L/home/vador/local/lib \
+
     -L$HOME/.local/lib \
     biohello.cpp -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core \
+
     -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core \
     -o biohello
+
     -o biohello \
 +
    biohello.cpp
 
strip biohello
 
strip biohello
 
</source>
 
</source>
 
Here again you may omit the PopGen and Phyl libraries.
 
Here again you may omit the PopGen and Phyl libraries.
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.
+
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.

Revision as of 11:24, 4 August 2012

This page explains how to compile a program that uses Bio++ (source file biohello.cpp) using g++. It is intended at linux users primarily.


In this example we assume that the libraries binaries (.so and/or .a files) are located in directory $HOME/.local/lib and that the library includes tree (.h files) are located in directory $HOME/.local/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.

Remarks
  • The CPATH/CPLUS_INCLUDE_PATH and LIBRARY_PATH environment variables can be used instead of the -I and -L parameters, respectively.
  • 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.

Dynamic linkage

<source lang="bash"> g++ -I$HOME/.local/include \

   -L$HOME/.local/lib \
   -lbpp-core -lbpp-seq -lbpp-phyl -lbpp-popgen \
   -o biohello \
   biohello.cpp

</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

When compiling with static linkage, the order of the libraries to include is important: <source lang="bash"> g++ --static

   -I$HOME/.local/include \
   -L$HOME/.local/lib \
   -lbpp-popgen -lbpp-phyl -lbpp-seq -lbpp-core \
   -o biohello \
   biohello.cpp 

strip biohello </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 strip command removes all unused code from the executable, and efficiently decreases its size.