Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add github library(BamTools) to c++ using cmake ExternalProject_Add

Tags:

c++

git

cmake

I would like to use the BamTools library for a project. I am using CMake ExternalProject_Add to do this. CMake clones and compiles BamTools GitHub repo fine, but no matter what I try I can't get it to link properly. Does anyone have any ideas?

Here is my CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(myProject)
include(ExternalProject)

set( CMAKE_C_FLAGS "-Wall -g")
set(BAMTOOLS_ROOT ${CMAKE_CURRENT_BINARY_DIR}/external/bamtools)
set(BAMTOOLS_INCLUDE_DIRS ${BAMTOOLS_ROOT}/include/bamtools)
set(BAMTOOLS_LIBRARIES ${BAMTOOLS_ROOT}/lib/bamtools/libbamtools.a)
set(bamtools_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/bamtools")
set(bamtools_CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${bamtools_INSTALL_DIR})

include_directories(${BAMTOOLS_INCLUDE_DIRS})

ExternalProject_Add(bamtools
  PREFIX ${BAMTOOLS_ROOT}
  GIT_REPOSITORY https://github.com/pezmaster31/bamtools
  BINARY_DIR ${BAMTOOLS_ROOT}
  INSTALL_DIR ${BAMTOOLS_ROOT}
  CMAKE_ARGS ${bamtools_CMAKE_ARGS}
)

add_subdirectory(src)

My simple main.cpp file exists in a src folder in the same directory as the CMakeLists.txt

#include <iostream>
#include <string>
#include <api/BamReader.h>

using namespace BamTools;

int main(){
    BamReader reader =BamReader();
    reader.Close();
};

I get the following error

Linking CXX executable project
Undefined symbols for architecture x86_64:
  "_crc32", referenced from:
      BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
  "_deflate", referenced from:
      BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
  "_deflateEnd", referenced from:
      BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
  "_deflateInit2_", referenced from:
      BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
  "_inflate", referenced from:
      BamTools::Internal::BgzfStream::InflateBlock(unsigned long const&) in libbamtools.a(BgzfStream_p.cpp.o)
  "_inflateEnd", referenced from:
      BamTools::Internal::BgzfStream::InflateBlock(unsigned long const&) in libbamtools.a(BgzfStream_p.cpp.o)
  "_inflateInit2_", referenced from:
      BamTools::Internal::BgzfStream::InflateBlock(unsigned long const&) in libbamtools.a(BgzfStream_p.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/project] Error 1
make[1]: *** [src/CMakeFiles/project.dir/all] Error 2
make: *** [all] Error 2
like image 904
Teancum Avatar asked Oct 01 '22 06:10

Teancum


1 Answers

Like the other answer said it was not linking ZLib properly. Making the following changes will resolve the issue. Instead of the add_subdirectory(src) add

find_package(ZLIB REQUIRED)
add_library(libbamtools STATIC IMPORTED)
set_target_properties(libbamtools PROPERTIES IMPORTED_LOCATION ${BAMTOOLS_LIBRARIES}/libbamtools.a)
add_dependencies(libbamtools bamtools)

add_executable(myProject src/main.cpp)

include_directories(${BAMTOOLS_INCLUDE_DIRS})
target_link_libraries(myProject libbamtools ${ZLIB_LIBRARIES})
like image 118
Teancum Avatar answered Oct 09 '22 02:10

Teancum