C ++如何使用CMakeList.txt链接mongoc和mongocxx静态库

时间:2017-11-15 06:47:32

标签: c++ mongodb cmake mongo-cxx-driver mongo-c-driver

我按照官方安装教程进行操作:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/ 我在make&&和make之前启用了标志--enable-static = yes和./configure sudo make install我得到了libmongoc-1.0.a和libbson-1.0.a这些都是静态的。我已经给了

cmake -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/your/cxxdriver/prefix
    -DLIBMONGOC_DIR=/your/cdriver/prefix
    -DLIBBSON_DIR=/your/cdriver/prefix .. 

然后制作&& sudo make install也完成了。所以到目前为止,一切都很好。

当我检查链接到我的最终可执行文件的库是

ldd ./prabhandhak 
    linux-vdso.so.1 =>  (0x00007fff6d1f0000)
    libbsoncxx.so._noabi => /usr/local/lib/libbsoncxx.so._noabi (0x00007f021cffb000)
    libmongocxx.so._noabi => /usr/local/lib/libmongocxx.so._noabi (0x00007f021cda4000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f021ca06000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f021c6fd000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f021c4e6000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f021c11c000)
    libbson-1.0.so.0 => /usr/local/lib/libbson-1.0.so.0 (0x00007f021bee5000)
    libmongoc-1.0.so.0 => /usr/local/lib/libmongoc-1.0.so.0 (0x00007f021bc6e000)
    /lib64/ld-linux-x86-64.so.2 (0x000055ef6575a000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f021ba51000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f021b848000)
    libsnappy.so.0 => /usr/local/lib/libsnappy.so.0 (0x00007f021b631000)

这里我想链接mongo静态库

这是我的cmakelist.txt

cmake_minimum_required (VERSION 3.5)

# -------------------------------------------------------------------------------------
## Checking if an env variable for "3rd party includes" is set.
## If not, we throw an error and exit.
#Message("Checking for environment variable" "IAM_EXT_INCLUDES")
#if (DEFINED ENV{IAM_EXT_INCLUDES})
#  Message ("found:" $ENV{IAM_EXT_INCLUDES})
#else()
#  Message(FATAL_ERROR "I need the environment variable IAM_EXT_INCLUDES to continue.")
#endif()
# -------------------------------------------------------------------------------------

project(mango VERSION 0.1.0)


# search for CPP_HOME. If found use this as the install_root dir.
# else, use /usr/local
message (STATUS "Searching for environment var CPP_HOME ...")
if (DEFINED ENV{CPP_HOME})
message (STATUS "Found CPP_HOME: " $ENV{CPP_HOME})
set (CPP_HOME $ENV{CPP_HOME})
else()
message (STATUS "Could not find.  Treating /usr/local as CPP_HOME...")
set (CPP_HOME /usr/local)
endif()
set(Boost_USE_STATIC_LIBS ON)
set(BOOST_LIBRARYDIR /usr/lib/x86_64-linux-gnu/)
# link_directories(/usr/lib/x86_64-linux-gnu/)
find_package(Boost REQUIRED COMPONENTS system date_time)
set(Boost_USE_STATIC_LIBS ON)
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
include_directories(${dbconn_SOURCE_DIR}/Implementation/mongo)
include_directories(${BOOST_INCLUDE_DIRS})
set (CMAKE_CXX_STANDARD 11)

if(libmongocxx_FOUND)
message("include path:" ${LIBMONGOCXX_INCLUDE_DIRS})
endif()
add_library(mongodbapi mongo_db_api.cpp)

# include third party lib headers (such as plog)
include_directories(/Volumes/second/nvi/github/externals/)
include_directories(../../../Interface/)
include_directories(./include/ext/)
include_directories(../../../../../utils/)
include_directories(${CPP_HOME}/externals/plog/include)
include_directories(${include_directories} ${LIBMONGOCXX_INCLUDE_DIRS})
include_directories(${include_directories} ${LIBBSONCXX_INCLUDE_DIRS})

target_link_libraries(mongodbapi ${LIBMONGOCXX_LIBRARIES})
target_link_libraries(mongodbapi ${LIBMONGOCXX_LIBRARIES})
target_link_libraries(mongodbapi ${Boost_LIBRARIES})


set (inst_dir_prefix mango-v${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
install (TARGETS mongodbapi DESTINATION ${inst_dir_prefix}/lib)
FILE(GLOB inc_files "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
install (FILES ${inc_files} DESTINATION ${inst_dir_prefix}/include)


FILE(GLOB utils_includes "../../../../../utils/*.hpp")
#install (FILES ${inc_files} DESTINATION ${inst_dir_prefix}/include)
INSTALL(FILES ${utils_includes} DESTINATION "${CPP_HOME}/utils")

请帮我链接静态库到cmakefile.txt

1 个答案:

答案 0 :(得分:0)

在mongocxx的CMakeLists.txt中看起来有相关的选项。所以

--- cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
+++ cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=False -DCMAKE_INSTALL_PREFIX=/usr/local ..  

(这是您教程的第4步)