使用cmake ExternalProject_Add将github库(BamTools)添加到c ++

时间:2014-07-21 23:03:07

标签: c++ git cmake

我想将BamTools库用于项目。我正在使用CMake ExternalProject_Add来执行此操作。 CMake克隆和编译BamTools GitHub repo罚款,但无论我尝试什么,我都无法正确链接。有没有人有任何想法?

这是我的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)

我的简单main.cpp文件存在于与CMakeLists.txt相同的目录中的src文件夹中

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

using namespace BamTools;

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

我收到以下错误

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

2 个答案:

答案 0 :(得分:4)

与其他答案一样,它说没有正确连接ZLib。进行以下更改将解决此问题。而不是add_subdirectory(src)添加

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})

答案 1 :(得分:0)

我相信这些是zlib的功能。您可能需要显式链接zlib。尝试添加find_package(ZLIB REQUIRED)