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?
I think the error might actually be that I passed
bamtools-master/lib
and notbamtools-master/src
to the-L
option of g++?The
-L
option takes a path to where ever the.so
file is found (the one calledlibbamtools.so.2.3.0
), so if it is inbamtools-master-src
then you are correct.