1

我按照官方教程进行安装:https ://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/ 我之前使用 ./configure 启用了标志 --enable-static=yes做 make && 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 .. 

然后 make && 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

4

1 回答 1

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

于 2017-11-15T08:11:57.847 回答