Linking errors when building c++ project using mongo-cxx-driver

回眸只為那壹抹淺笑 提交于 2020-01-15 01:53:07

问题


I am currently developing a C++ application which requires the use of the mongo-cxx-driver for accessing a MongoDB instance. I attempted a couple of methods of installation, and am met with the same linker issues each time.

Initially, I attempted to install mongo-cxx-drivers and mongod-c-driver as detailed here: https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/

Using the following portion of my CMake configuration, I was able to get auto-completion working and my IDE to recognize the libraries:

. . .

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})

. . .

Unfortunately during the linking stage, I get these errors:

[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

I tried building using different c++17 polyfills just in case, no go. I also tried manually uninstalling mongo-cxx-driver and mongo-c-driver this time installing thru homebrew, but was met with the same errors.

In my research, the most relatable StackOverflow post is Using the mongodb cxx driver in a cmake c++ project, yet none of the solutions there work for me.

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

Any help or insight would be much appreciated, please feel free to ask me to clarify or add information where it may be unclear or missing.

EDIT: Here is the code which is causing the error:

#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);

EDIT: I went ahead and created a simple project from scratch and went through all the steps again just to be sure I didn't flub anything. Still in the same boat at the end though.

/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]

Here is the full output when building with verbose mode enabled for 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

The full 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})

The code producing the errors:

#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;
}

回答1:


Turns out I was using CMake's GLOB wrong.

Changing

target_link_libraries(App ${COMMON_LIBRARIES})

to

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

fixed the issue.



来源:https://stackoverflow.com/questions/46356623/linking-errors-when-building-c-project-using-mongo-cxx-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!