Я пытаюсь создать простое приложение 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
)
Я собрал mongocxx и зависимые от него библиотекиUbuntu 14.04 примерно так:
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