静态链接mongocxx缺少sasl

时间:2019-03-03 10:27:35

标签: cmake linker-errors sasl mongo-cxx-driver

我正在尝试构建一个简单的mongocxx应用程序,并且出现链接错误:

-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.8") 
-- Configuring done
-- Generating done
-- Build files have been written to: /code/build
[ 50%] Linking CXX executable test_new_mongo
../mongo_cxx_driver/lib/libmongoc-static-1.0.a(mongoc-client.c.o): In function `_mongoc_get_rr_search':
mongoc-client.c:(.text+0xa1): undefined reference to `__res_nsearch'
mongoc-client.c:(.text+0xbd): undefined reference to `ns_initparse'
mongoc-client.c:(.text+0x11e): undefined reference to `ns_parserr'
../mongo_cxx_driver/lib/libmongoc-static-1.0.a(mongoc-client.c.o): In function `srv_callback':
mongoc-client.c:(.text+0x3d0): undefined reference to `__dn_expand'
collect2: error: ld returned 1 exit status
make[2]: *** [test_new_mongo] Error 1
make[1]: *** [CMakeFiles/test_new_mongo.dir/all] Error 2
make: *** [all] Error 2

请。如何解决这些链接错误?

main.cpp:

#include "mongocxx/instance.hpp"

int main() {
    mongocxx::instance inst{};
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.13)
project(test_new_mongo)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

find_library(BSONC NAMES "bson-static-1.0" PATHS mongo_cxx_driver/lib)
find_library(BSONCXX_CLIENT NAMES "bsoncxx-static" PATHS mongo_cxx_driver/lib)
find_library(MONGOC_CLIENT NAMES "mongoc-static-1.0" PATHS mongo_cxx_driver/lib)
find_library(MONGO_CLIENT NAMES "mongocxx-static" PATHS mongo_cxx_driver/lib)

find_library(SASL_SHARED_LIB NAMES "libsasl2${CMAKE_SHARED_LIBRARY_SUFFIX}")
find_package(OpenSSL REQUIRED)
set(THREADS_PREFER_PTHREAD_FLAG ON)
FIND_PACKAGE ( Threads REQUIRED )
find_package(ZLIB REQUIRED)

include_directories(mongo_cxx_driver/include)

add_executable(test_new_mongo main.cpp)


target_link_libraries(test_new_mongo
        ${MONGO_CLIENT}
        ${MONGOC_CLIENT}
        ${BSONCXX_CLIENT}
        ${BSONC}
        ${SASL_SHARED_LIB}
        ${OPENSSL_LIBRARIES}
        "${CMAKE_THREAD_LIBES_INIT}"
        ${ZLIB_LIBRARIES}
        rt
        )

我在ubuntu 14.04上构建了mongocxx及其依赖库,如下所示:

apt-get -y update && apt-get -y install \
g++ \
wget \
build-essential \
autoconf \
automake \
autotools-dev \
dh-make \
debhelper \
devscripts \
fakeroot \
xutils \
lintian \
git \
python-dev \
python-pip \
software-properties-common \
pkg-config \
libssl-dev \
openssh-server \
libsasl2-dev \
zlib1g-dev


add-apt-repository ppa:ubuntu-toolchain-r/test -y && \
apt-get update -y && \
apt-get install -y --force-yes gcc-8 g++-8 -y && \
update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 60 --slave /usr/bin/g++ g++ /usr/bin/g++-8


wget https://cmake.org/files/v3.13/cmake-3.13.0-Linux-x86_64.sh && \
chmod +x cmake-3.13.0-Linux* && \
./cmake-3.13.0-Linux-x86_64.sh --skip-license


cd ~ \
&& wget https://github.com/mongodb/mongo-c-driver/releases/download/1.14.0/mongo-c-driver-1.14.0.tar.gz \
&& tar xzf mongo-c-driver-*.tar.gz \
&& cd mongo-c-driver-* \
&& mkdir cmake-build \
&& cd cmake-build \
&& cmake -DCMAKE_CXX_STANDARD=17 -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release .. \
&& make \
&& make install

下一步是构建mongocxx,但是在构建时出现了错误,我通过应用来自这两个PR的补丁来修复了这些错误: https://jira.mongodb.org/browse/CXX-1688 https://jira.mongodb.org/browse/CXX-1689

然后我继续使用以下方法成功构建:

cd ~ \
&& git clone https://github.com/mongodb/mongo-cxx-driver.git --branch releases/stable --depth 1 \
&& cd mongo-cxx-driver/build \
&& cmake  -DCMAKE_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_PREFIX_PATH=/usr/local -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF.. \
&& make \
&& make install 

然后我将libbson-static-1.0.a,libbsoncxx-static.a,libmongoc-static-1.0.a和libmongocxx-static.a复制到mongo_cxx_drive / lib的项目文件夹下 并且还将标头bsoncxx和mongocxx复制到mongo_cxx_driver / include

1 个答案:

答案 0 :(得分:0)

未定义的引用属于库解析。 在target_link_libraries的末尾添加“ resolv”。像这样:

target_link_libraries(test_new_mongo
        ${MONGO_CLIENT}
        ${MONGOC_CLIENT}
        ${BSONCXX_CLIENT}
        ${BSONC}
        ${SASL_SHARED_LIB}
        ${OPENSSL_LIBRARIES}
        "${CMAKE_THREAD_LIBES_INIT}"
        ${ZLIB_LIBRARIES}
        rt
        resolv
        )