This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Trouble Compiling C++ Code With Bamtools

I'm trying to link C++ code to the bamtools library. My directory structure is:

myproject/
  Makefile
  lib/
    bamtools-master/
  include/
  src/bam_example.cpp

The lib/bamtools-master/ file is the contents of https://github.com/pezmaster31/bamtools/archive/master.zip

My code is:

include "api/BamMultiReader.h"
#include "api/BamWriter.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace BamTools;
BamMultiReader reader;

int main(int argc, char* argv[]) {
  cerr << "Test";
}

The Makefile is:

bam_example:
    g++ -I ./lib/bamtools-master/include/ -L ./lib/bamtools-master/lib/ -o bam_example src/bam_example.cpp -lz -lbamtools

clean:
    rm bam_example

When I compile I get:

$ make
g++ -I ./lib/bamtools-master/include/ -L ./lib/bamtools-master/lib/ -o bam_example src/bam_example.cpp -lz -lbamtools
$ ./bam_example 
./bam_example: error while loading shared libraries: libbamtools.so.2.3.0: cannot open shared object file: No such file or directory

What went wrong here? What is the correct way to link to bamtools library?

What is wrong with this example?

samtools rna-seq

2 answers

I think your problem may be that the ./lib/bamtools-master/lib/ directory is not part of your LD_LIBRARY_PATH environmental variable. try typing export LD_LIBRARY_PATH=<PREFIX>/lib/bamtools-master/lib/:$LD_LIBRARY_PATH at the prompt then trying to run the command again.

I think the error might actually be that I passed bamtools-master/lib and not bamtools-master/src to the -L option of g++?

The -L option takes a path to where ever the .so file is found (the one called libbamtools.so.2.3.0), so if it is in bamtools-master-src then you are correct.

I've got in a same situation when I use MaLTA, that performs bamtools internally.I saw http://www.vcru.wisc.edu/simonlab/bioinformatics/programs/install/bamtools.html, but its section 9 is incollect.

Pasadena is right(adding /lib/). Thank you.

Log in to answer this question.