我安装了mongocxx驱动程序,如http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/所示, 当我测试驱动程序时,一切看起来都很好,但是如果更改一点代码,则在加载共享库时出现错误:libbsoncxx.so._noabi:无法打开共享对象文件:没有这样的文件或目录。
代码:
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
if(POLICY CMP0025)
cmake_policy(SET CMP0025 NEW)
endif()
project(test_mongodb LANGUAGES C CXX)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_PREFIX_PATH CMAKE_PREFIX_PATH "~/opt/mongo-cxx-driver/install")
set(CMAKE_CXX_EXTENSIONS OFF)
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror")
endif()
add_executable(test_mongodb main.cpp)
find_package(libmongocxx REQUIRED)
target_include_directories(test_mongodb
PRIVATE ${LIBMONGOCXX_INCLUDE_DIRS}
)
target_link_libraries(test_mongodb
PRIVATE ${LIBMONGOCXX_LIBRARIES}
)
target_compile_definitions(test_mongodb
PRIVATE ${LIBMONGOCXX_DEFINITIONS}
)
add_custom_target(run
COMMAND test_mongodb
DEPENDS test_mongodb
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
)
list(FIND LIBMONGOCXX_DEFINITIONS "BSONCXX_STATIC" LIST_IDX)
if (${LIST_IDX} GREATER -1)
message(FATAL_ERROR "Expected BSONCXX_STATIC to not be defined")
endif()
list(FIND LIBMONGOCXX_DEFINITIONS "MONGOCXX_STATIC" LIST_IDX)
if (${LIST_IDX} GREATER -1)
message(FATAL_ERROR "Expected MONGOCXX_STATIC to not be defined")
endif()
main.cpp
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/cursor.hpp>
int main(int argc, char* argv[]) {
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
mongocxx::instance inst;
try {
const auto uri = mongocxx::uri{(argc >= 2) ? argv[1] : mongocxx::uri::k_default_uri};
auto client = mongocxx::client{uri};
auto admin = client["admin"];
auto result = admin.run_command(make_document(kvp("isMaster", 1)));
std::cout << bsoncxx::to_json(result) << std::endl;
return EXIT_SUCCESS;
} catch (const std::exception& xcp) {
std::cout << "connection failed: " << xcp.what() << std::endl;
return EXIT_FAILURE;
}
}
如果我从代码中删除auto result = admin.run_command(make_document(kvp("isMaster", 1)));
,则在加载共享库libbsoncxx.so._noabi 时出现错误,如果不能正常工作。知道为什么它会那样吗?
Ubuntu 18.04 cc 7.3.0, C ++ 7.3.0, CMake 3.11.3
答案 0 :(得分:0)
您正在以太低的级别进入CMake find
子系统:Mongo C ++驱动程序提供了与CMake find_package子系统的集成,该子系统为您完成了许多工作。您应该只需说出find_package(libmongocxx REQUIRED)
,然后使用预先填充的变量LIBMONGOCXX_{INCLUDE_DIRS,LIBRARIES,DEFINITIONS}
。
对于许多示例项目,请参见源代码中的顶级examples/projects
目录,这些示例项目说明如何通过CMake的{mongo,bson}cxx
和find_package
与pkgconfig
进行集成。
我认为,鉴于您在上面针对的用途,您应该从目录examples/projects/mongocxx/cmake/shared
开始。
答案 1 :(得分:0)
当我使用readelf时,我收到了:
File: libmongocxx.so
(NEEDED) Shared library: [libbsoncxx.so._noabi]
(NEEDED) Shared library: [libmongoc-1.0.so.0]
(NEEDED) Shared library: [libbson-1.0.so.0]
(NEEDED) Shared library: [libstdc++.so.6]
(NEEDED) Shared library: [libgcc_s.so.1]
(NEEDED) Shared library: [libc.so.6]
(SONAME) Library soname: [libmongocxx.so._noabi]
(RUNPATH) Library runpath: [/home/adam/opt/mongo-c-driver/install/lib]
File: test_mongodb ( with error )
(NEEDED) Shared library: [libmongocxx.so._noabi]
(NEEDED) Shared library: [libstdc++.so.6]
(NEEDED) Shared library: [libc.so.6]
(RUNPATH) Library runpath: [/home/adam/opt/mongo-cxx-driver/install/lib]
File: test_mongodb ( without error )
(NEEDED) Shared library: [libmongocxx.so._noabi]
(NEEDED) Shared library: [libbsoncxx.so._noabi]
(NEEDED) Shared library: [libstdc++.so.6]
(NEEDED) Shared library: [libgcc_s.so.1]
(NEEDED) Shared library: [libc.so.6]
(RUNPATH) Library runpath: [/home/adam/opt/mongo-cxx-driver/install/lib]
答案 2 :(得分:0)
https://www.cnblogs.com/pluse/p/5491300.html 对于erros而言,reson是关于库链接的。只需重新链接库,如下所示就可以解决问题
sudo ln -s /usr/local/lib/libmogocxx.so.3.4.0 libmongocxx.so._noabi
sudo ln -s /usr/local/lib/libmongocxx.so._noabi /usr/local/lib/libmongocxx.so