问题描述
我正在尝试构建一个简单的mongocxx应用程序,并且出现链接错误:
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?
main.cpp:
#include "mongocxx/instance.hpp"
int main() {
mongocxx::instance inst{};
return 0;
}
CMakeLists.txt:
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
)
我在ubuntu 14.04上构建了mongocxx及其依赖库,如下所示:
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
下一步是构建mongocxx,但是在构建时出现了错误,我通过应用来自这两个PR的补丁来修复了这些错误: https://jira.mongodb.org/browse/CXX-1688 https://jira.mongodb.org/browse/CXX-1689
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-1688https://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
然后我将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
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/liband also copied the headers bsoncxx and mongocxx under mongo_cxx_driver/include
推荐答案
未定义的引用属于库解析.在target_link_libraries的末尾添加"resolv".像这样:
The undefined reference belong to the library resolv.Add "resolv" to the end of the target_link_libraries. Like so:
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
)
这篇关于静态链接mongocxx缺少sasl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!