使用 mongo-cxx-driver 构建 C++ 项目时出现链接错误

2024-01-05

我目前正在开发一个C++需要使用的应用程序mongo-cxx-driver用于访问MongoDB实例。我尝试了几种安装方法,但每次都会遇到相同的链接器问题。

最初,我尝试安装mongo-cxx-drivers and mongod-c-driver详细信息如下:https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/ https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/

使用 CMake 配置的以下部分,我能够自动完成工作,并且我的 IDE 能够识别库:

. . .

set(CMAKE_CXX_STANDARD 17)

set(BUILD_DIR "cmake-build-debug")
set(BUILD_PATH "${CMAKE_SOURCE_DIR}/${BUILD_DIR}")

find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)

message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")

message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")

file(GLOB COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})

SET(APP_SOURCE source/App/main.cpp)

SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${BUILD_PATH}/App)
add_executable(App ${APP_SOURCE})
target_include_directories(App PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(App PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})

target_link_libraries(App ${COMMON_LIBRARIES})

. . .

不幸的是,在链接阶段,我收到以下错误:

[100%] Linking CXX executable App/App
    Undefined symbols for architecture x86_64:
  "mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)", referenced from:
      App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
  "mongocxx::v_noabi::uri::~uri()", referenced from:
      App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
  "mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)", referenced from:
      App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
  "mongocxx::v_noabi::client::~client()", referenced from:
      App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in .cpp.o
  "mongocxx::v_noabi::instance::instance()", referenced from:
      App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
  "mongocxx::v_noabi::instance::~instance()", referenced from:
      App::Initialize(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [App/App] Error 1
make[2]: *** [CMakeFiles/App.dir/all] Error 2
make[1]: *** [CMakeFiles/App.dir/rule] Error 2
make: *** [App] Error 2

我尝试使用不同的构建c++17以防万一,不去。我也尝试过手动卸载mongo-cxx-driver and mongo-c-driver这次通过自制程序安装,但遇到了同样的错误。

根据我的研究,最相关的 StackOverflow 帖子是在 cmake c++ 项目中使用 mongodb cxx 驱动程序 https://stackoverflow.com/questions/37210716/using-the-mongodb-cxx-driver-in-a-cmake-c-project,但那里的解决方案都不适合我。

Operating System: macOS Sierra 10.12.6
IDE: CLion 2017.2.2 Build #CL-172.3968.17, built on August 22, 2017
CMake: 3.8.2
mongo-cxx-driver: 3.1.3
mongo-c-driver: 1.8.0

任何帮助或见解将不胜感激,请随时要求我澄清或添加可能不清楚或缺失的信息。

编辑:这是导致错误的代码:

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;

mongocxx::instance instance{}; // This should be done only once.
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client client(uri);

编辑:我继续从头开始创建一个简单的项目,并再次完成所有步骤,以确保我没有搞砸任何事情。但最终还是在同一条船上。

/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/user000/Projects/mongo-cxx-driver-test
-- CMAKE_SOURCE_DIR: /Users/user000/Projects/mongo-cxx-driver-test
-- BUILD_PATH: /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug
LIBMONGOCXX_INCLUDE_DIRS = /usr/local/include/mongocxx/v_noabi
LIBMONGOCXX_LIBRARIES = mongocxx
LIBBSONCXX_INCLUDE_DIRS = /usr/local/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = bsoncxx
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug

[Finished]

以下是在为 CMake 启用详细模式的情况下构建时的完整输出:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug --target App -- -j 2
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/user000/Projects/mongo-cxx-driver-test -B/Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 App
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/user000/Projects/mongo-cxx-driver-test -B/Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug/CMakeFiles 2
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 CMakeFiles/App.dir/all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/App.dir/build.make CMakeFiles/App.dir/depend
cd /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/user000/Projects/mongo-cxx-driver-test /Users/user000/Projects/mongo-cxx-driver-test /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug/CMakeFiles/App.dir/DependInfo.cmake --color=
Scanning dependencies of target App
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/App.dir/build.make CMakeFiles/App.dir/build
[ 50%] Building CXX object CMakeFiles/App.dir/source/App/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++   -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi  -g   -std=gnu++1z -o CMakeFiles/App.dir/source/App/main.cpp.o -c /Users/user000/Projects/mongo-cxx-driver-test/cmake-build-debug/source/App/main.cpp
[100%] Linking CXX executable App
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/App.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/App.dir/source/App/main.cpp.o  -o App 
Undefined symbols for architecture x86_64:
  "mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)", referenced from:
      _main in main.cpp.o
  "mongocxx::v_noabi::uri::~uri()", referenced from:
      _main in main.cpp.o
  "mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)", referenced from:
      _main in main.cpp.o
  "mongocxx::v_noabi::client::~client()", referenced from:
      _main in main.cpp.o
  "mongocxx::v_noabi::instance::instance()", referenced from:
      _main in main.cpp.o
  "mongocxx::v_noabi::instance::~instance()", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [App] Error 1
make[2]: *** [CMakeFiles/App.dir/all] Error 2
make[1]: *** [CMakeFiles/App.dir/rule] Error 2
make: *** [App] Error 2

完整的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.8)
project(App)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE on)

set(BUILD_DIR "cmake-build-debug")
set(BUILD_PATH "${CMAKE_SOURCE_DIR}/${BUILD_DIR}")

set(BUILD_DIR "cmake-build-debug")
set(BUILD_PATH "${CMAKE_SOURCE_DIR}/${BUILD_DIR}")

message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(STATUS "BUILD_PATH: ${BUILD_PATH}")

find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)

message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")

message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")

file(GLOB COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})

set(SOURCE_FILES cmake-build-debug/source/App/main.cpp)

add_executable(App ${SOURCE_FILES})
target_include_directories(App PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(App PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_link_libraries(App ${COMMON_LIBRARIES})

产生错误的代码:

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <mongocxx/stdx.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;

int main() {
  std::cout << "Hello, World!" << std::endl;

  mongocxx::instance instance{}; // This should be done only once.
  mongocxx::uri uri("mongodb://localhost:27017");
  mongocxx::client client(uri);

  return 0;
}

原来我使用 CMake 的 GLOB 是错误的。

改变

target_link_libraries(App ${COMMON_LIBRARIES})

to

target_link_libraries(App ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})

解决了这个问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 mongo-cxx-driver 构建 C++ 项目时出现链接错误 的相关文章

随机推荐

  • 将“memberOf”属性添加到 ApacheDS

    我正在尝试在 Apache Directory 中模拟 Active Directory 的 memberOf 属性 我已将 memberOf 的以下条目添加到我的 LDIF 文件中 dn m oid 1 3 6 1 4 1 18060 0
  • 检查 url 是否包含 http:// 或 https:// [重复]

    这个问题在这里已经有答案了 可能的重复 检查 url 是否包含 http 或 https https stackoverflow com questions 7334491 check if the url is contains the
  • 如何在加载时打开 React Native Maps 标记的标注

    我希望在安装屏幕组件时打开所有标记的所有标注 目前 它仅在单击标记时打开 如何在功能组件中使用 useRef 来执行此操作 const markerRef useRef React createRef return
  • 使用 C++17 Constexpr 查找数组

    我正在尝试编写一个 constexpr find 函数 它将返回包含特定值的 std array 的索引 下面的函数似乎工作正常 除非包含的类型是const char include
  • 哪个班级设计比较好? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 哪个类设计更好 为什么 public class User public String UserName public String
  • 在 OS X 上以管理员权限运行脚本

    我已经尽力在 Stack Overflow 和互联网上找到许多脚本问题的解决方案 但我似乎找不到我需要的解决方案 我想要做的是创建一个更加自动化且点击次数更少的解决方案来删除系统上的所有移动缓存用户帐户 我一直在登录并手动转到用户帐户 然后
  • 如何设置 clojureScript 项目以使用规范并在运行时测试 clojure.core 函数?

    Clojure 1 9 推出specs https clojure org guides spec clojure core 库中的函数现在有规范 如何设置 clojurescript 项目以使用规范并在运行时测试 clojure core
  • 我可以采取什么措施来加快 S3 上传/更新速度?

    今天我一整天都在尝试向 s3 上传一些小东西 500 个目录中约有 20k 个文件 总计约 3GB 对于名为 简单存储服务 的服务来说 这是绝对合理的 我可以平均以大约 500k s 1mb s 1 8 到 3 6 GB h 之间 的速度上
  • Java 中最好的企业购物车是什么? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 未针对早午餐编译供应商 CSS 文件

    我对 b 有疑问 电子邮件受保护 cdn cgi l email protection不编译 Bower Component CSS 文件 如同在 Brunch 中分离应用程序和供应商 CSS https stackoverflow com
  • 在 C 中创建数组时出现分段错误

    我最近迁移到一台新笔记本电脑 HP dv6119tx 英特尔酷睿 i5 4 GB RAM 它安装了 Windows 7 Home Premium 64 位 我正在尝试创建一个类型的数组int长度为 10 6 的 C Dev C 我曾经在我的
  • 在 React Native 中使用 PanResponder 锁定移动

    使用本机反应泛响应器 https facebook github io react native docs panresponder html 当屏幕触摸坐标超出一定值范围时 如何阻止移动 例如 如何防止用户将组件移动到屏幕上某个 y 位置
  • 比较堆转储 (HPROF) 文件

    是否可以比较两个 HPROF 文件 如何 根据我的发现 您只能比较对象的直方图 为此 请转到 直方图 视图 然后单击 与另一个堆转储比较 并选择另一个 hprof 文件 Here is screenshot
  • 获取孩子的所有孩子等等

    我使用 MongoDb 作为数据库 我想要所有孩子的孩子等等 让我们假设 A 有 B 和 C 孩子 B 有 D 和 E 孩子 D 有 F 和 G 孩子 所以当我查询子节点时A 我将所有孩子作为输出 例如 B C D E F G C Cust
  • 检查一个数据帧的值是否按确切顺序存在于另一个数据帧中

    我有 1 个数据数据框和多个 参考 数据框 我正在尝试自动检查数据帧的值是否与参考数据帧的值匹配 重要的是 这些值的顺序也必须与参考数据帧中的值相同 这些列是重要的列 但我的真实数据集包含更多列 下面是一个玩具数据集 Dataframe g
  • 1个月后自动将列表数据从一个列表复制到另一个列表

    我列出了在提交信息路径表单后动态存储数据的列表 我想在任何数据创建日期 30 天后存档此数据 你能建议我该怎么做吗 看看我可以通过工作流程做到这一点 但我如何设置条件 在创建任何列表后 30 天完成后 它将自动复制到其他列表中 首先我想问为
  • 如何防止XSS攻击

    渗透测试团队告诉我 以下 URL 正在引发 XSS 攻击 这是我的 download msg jsp 代码
  • 存储过程参数默认值

    我正在尝试创建一个带有默认参数的存储过程 在我的查询中我会这样做 DECLARE mydate DATETIME DECLARE MT DATETIME DECLARE MY DATETIME SELECT mydate GETDATE S
  • 填充seaborn / matplotlib中两个正态分布之间的重叠区域

    我想填充两个正态分布之间重叠的区域 我有x最小值和最大值 但我不知道如何设置y边界 我看过plt文档 https matplotlib org gallery lines bars and markers fill between demo
  • 使用 mongo-cxx-driver 构建 C++ 项目时出现链接错误

    我目前正在开发一个C 需要使用的应用程序mongo cxx driver用于访问MongoDB实例 我尝试了几种安装方法 但每次都会遇到相同的链接器问题 最初 我尝试安装mongo cxx drivers and mongod c driv