I am trying to build a simple mongocxx application and I am having linking errors:
-- 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
Please. How do I fix these linking errors?
The main.cpp:
#include "mongocxx/instance.hpp"
int main() {
mongocxx::instance inst{};
return 0;
}
The 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
)
I built mongocxx and its dependent libraries on ubuntu 14.04 like so:
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
The next step is to build mongocxx but upon building I got errors which I fixed by applying the patches from these two PRs: https://jira.mongodb.org/browse/CXX-1688 https://jira.mongodb.org/browse/CXX-1689
Then I proceeded to build successfully using:
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
I then copied the libbson-static-1.0.a, libbsoncxx-static.a, libmongoc-static-1.0.a and libmongocxx-static.a under the project folder of mongo_cxx_drive/lib and also copied the headers bsoncxx and mongocxx under mongo_cxx_driver/include