Ubuntu上mongo以及C++驱动的安装 error while loading shared libraries: libmongocxx.so._noabi: cannot open shar

1.安装mongodb

$sudo apt-get install mongodb

2。这样就安装成功了

hangwei@ubuntu:/etc/network$ mongo -version
MongoDB shell version v3.6.8
git version: 8e540c0b6db93ce994cc548f000900bdc740f80a
OpenSSL version: OpenSSL 1.1.1f  31 Mar 2020
allocator: tcmalloc
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

3设置mongo自启动
我尝试重启了一次 不需要设置开机启动 服务直接是开的

4.更新本地的包

sudo apt-get update

安装c驱动依赖的库

sudo apt-get update
sudo apt-get install -y cmake gcc git libsasl2-dev libssl-dev libsnappy-dev make pkg-config tar wget build-essential automake autoconf libtool

···

安装mongo依赖的库
```c
sudo apt-get install libmongoc-1.0-0
sudo apt-get install libbson-1.0

在ubuntu上构建C驱动 下载到 cd /opt/softwares

wget https://github.com/mongodb/mongo-c-driver/releases/download/1.16.0/mongo-c-driver-1.16.0.tar.gz
tar xzf mongo-c-driver-1.16.0.tar.gz -C /opt/modules	
cd mongo-c-driver-1.16.0/
mkdir cmake-build
cd cmake-build
sudo apt install cmake
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..

最后一行输出

-- Build files have been written to: /home/lzj/Downloads/mongo-c-driver-1.16.0/cmake-build

安装c驱动

sudo make install

生成并安装 mongoC++程序

cd $SOURCE_ROOT
git clone https://github.com/mongodb/mongo-cxx-driver.git
cd mongo-cxx-driver
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo make EP_mnmlstc_core

生成并安装驱动程序

make
sudo make install

测试C++是否安装成功

#include 

#include 
#include 

#include 
#include 

int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri("mongodb://localhost:27017")};

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
}

设置环境变量
编辑 /etc/profile

export LD_LIBRARY_PATH=/usr/local/lib
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

编译运行C++文件

c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
./test

按照这套方法做下来 我在运行的时候出现了问题

root@ubuntu:/home/zhangwei/Desktop# c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
root@ubuntu:/home/zhangwei/Desktop# ./test
./test: error while loading shared libraries: libmongocxx.so._noabi: cannot open shared object file: No such file or directory

Ubuntu上mongo以及C++驱动的安装 error while loading shared libraries: libmongocxx.so._noabi: cannot open shar_第1张图片

这段意思是少libmongocxx.so._noabi动态库

然后使用ldd 查看使用了什么库和缺少什么库
Ubuntu上mongo以及C++驱动的安装 error while loading shared libraries: libmongocxx.so._noabi: cannot open shar_第2张图片
上图可以看出来少了libmongocxx.so._noabi和libbsoncxx.so._noabi这两个库
查一下这两个库在哪里,发现他是个链接文件

oot@ubuntu:/home/zhangwei/Desktop# find / -name libmongocxx.so._noabi 2>/dev/null
/usr/local/lib/libmongocxx.so._noabi
/root/mongo-cxx-driver/build/src/mongocxx/libmongocxx.so._noabi
root@ubuntu:/home/zhangwei/Desktop# ls -l /usr/local/lib/libmongocxx.so._noabi
lrwxrwxrwx 1 root root 20 Apr  6 02:24 /usr/local/lib/libmongocxx.so._noabi -> libmongocxx.so.3.4.0
root@ubuntu:/home/zhangwei/Desktop# find / -name libbsoncxx.so._noabi 2>/dev/null
/usr/local/lib/libbsoncxx.so._noabi
/root/mongo-cxx-driver/build/src/bsoncxx/libbsoncxx.so._noabi
root@ubuntu:/home/zhangwei/Desktop# ls -l /usr/local/lib/libbsoncxx.so._noabi
lrwxrwxrwx 1 root root 19 Apr  6 02:24 /usr/local/lib/libbsoncxx.so._noabi -> libbsoncxx.so.3.4.0
root@ubuntu:/home/zhangwei/Desktop# ^C
root@ubuntu:/home/zhangwei/Desktop# cd /usr/local/lib/
root@ubuntu:/usr/local/lib# ls
cmake                 libbsoncxx.so.3.4.0     libmongoc-static-1.0.a
libbson-1.0.la        libbsoncxx.so._noabi    libmongocxx.so
libbson-1.0.so        libbson-static-1.0.a    libmongocxx.so.3.4.0
libbson-1.0.so.0      libmongoc-1.0.so        libmongocxx.so._noabi
libbson-1.0.so.0.0.0  libmongoc-1.0.so.0      pkgconfig
libbsoncxx.so         libmongoc-1.0.so.0.0.0  python3.8

可以看到这两个库我已经有了libmongocxx.so._noabi和 libbsoncxx.so._noabi 库也是一样的情况,看来是符号链接的问题,

root@ubuntu:/usr/local/lib# sudo rm /usr/local/lib/lib*.so._noabi
root@ubuntu:/usr/local/lib# ls
cmake                 libbsoncxx.so.3.4.0     libmongocxx.so
libbson-1.0.la        libbson-static-1.0.a    libmongocxx.so.3.4.0
libbson-1.0.so        libmongoc-1.0.so        pkgconfig
libbson-1.0.so.0      libmongoc-1.0.so.0      python3.8
libbson-1.0.so.0.0.0  libmongoc-1.0.so.0.0.0
libbsoncxx.so         libmongoc-static-1.0.a

重新手动链接

root@ubuntu:/usr/local/lib# sudo ln -s /usr/local/lib/libmongocxx.so.3.4.0 /usr/local/lib/libmongocxx.so._noabi
root@ubuntu:/usr/local/lib# sudo ln -s /usr/local/lib/libbsoncxx.so.3.4.0 /usr/local/lib/libbsoncxx.so._noabi
root@ubuntu:/usr/local/lib# ls
cmake                 libbsoncxx.so.3.4.0     libmongoc-static-1.0.a
libbson-1.0.la        libbsoncxx.so._noabi    libmongocxx.so
libbson-1.0.so        libbson-static-1.0.a    libmongocxx.so.3.4.0
libbson-1.0.so.0      libmongoc-1.0.so        libmongocxx.so._noabi
libbson-1.0.so.0.0.0  libmongoc-1.0.so.0      pkgconfig
libbsoncxx.so         libmongoc-1.0.so.0.0.0  python3.8

重新加载一下整个库,然后再次查看 test 所需的库的情况,可以看到,此时 hellomongo 以及能够找到所需的库了,如下图:

root@ubuntu:/usr/local/lib# sudo ldconfig
root@ubuntu:/usr/local/lib# cd /home/zhangwei/Desktop/
root@ubuntu:/home/zhangwei/Desktop# ls
test  test.cpp
root@ubuntu:/home/zhangwei/Desktop# ldd test
	linux-vdso.so.1 (0x00007fff6f152000)
	libmongocxx.so._noabi => /usr/local/lib/libmongocxx.so._noabi (0x00007f1326728000)
	libbsoncxx.so._noabi => /usr/local/lib/libbsoncxx.so._noabi (0x00007f1326706000)
	libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f1326524000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f1326509000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f1326317000)
	libmongoc-1.0.so.0 => /usr/local/lib/libmongoc-1.0.so.0 (0x00007f132625f000)
	libbson-1.0.so.0 => /usr/local/lib/libbson-1.0.so.0 (0x00007f132621e000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f13260cf000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f13267d8000)
	libsasl2.so.2 => /lib/x86_64-linux-gnu/libsasl2.so.2 (0x00007f13260b2000)
	libssl.so.1.1 => /lib/x86_64-linux-gnu/libssl.so.1.1 (0x00007f132601f000)
	libcrypto.so.1.1 => /lib/x86_64-linux-gnu/libcrypto.so.1.1 (0x00007f1325d49000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f1325d3f000)
	libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007f1325d21000)
	libsnappy.so.1 => /lib/x86_64-linux-gnu/libsnappy.so.1 (0x00007f1325d16000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f1325cf3000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f1325ced000)

这样就结束了 两天的时间 我那个去

root@ubuntu:/home/zhangwei/Desktop# ./test
{ "_id" : { "$oid" : "624e3689c32be7395b4c1122" }, "hello" : "world" }

参考:https://www.cnblogs.com/pluse/p/5491300.html
参考:https://blog.csdn.net/m0_38059875/article/details/104157215

你可能感兴趣的:(学习,c++,mongodb,ubuntu)