[CXX-1437] inking errors when building c++ project using mongo-cxx-driver Created: 22/Sep/17  Updated: 14/Nov/17  Resolved: 22/Sep/17

Status: Closed
Project: C++ Driver
Component/s: None
Affects Version/s: 3.1.3
Fix Version/s: None

Type: Bug Priority: Trivial - P5
Reporter: David Newman Assignee: Andrew Morrow (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

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



 Description   

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.

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

Note: I also have opened a StackOverflow question as this is a fairly time sensitive issue for me.



 Comments   
Comment by David Newman [ 22/Sep/17 ]

Works like a charm Guess I better go reread the docs for CMakes GLOB. Strange because I have been using it like that to bundle libraries in a few projects and didn't have a problem (yet, probably). Anyway thank you kindly for the help. Sorry for being a dunce

Comment by Andrew Morrow (Inactive) [ 22/Sep/17 ]

OK, so based on this line in that output:

/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

It doesn't look to me like the mongocxx or bsoncxx libraries are on the link line. I suspect something is going wrong with your COMMON_LIBRARIES variable.

Can you try changing this line

target_link_libraries(App ${COMMON_LIBRARIES})

To

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

And re-post the verbose build output?

Comment by David Newman [ 22/Sep/17 ]

Thanks for taking a look, it's much appreciated. I have XCode 9.0 installed currently. I should have thought to include that hehe . It was late and I was beating my head against the wall at that point.

Anyway, I went ahead and created a simple project from scratch this morning 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

Here is the CMake output for the project:

/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 itself 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;
}

If there is anything else you need on my part, let me know.

Comment by Andrew Morrow (Inactive) [ 22/Sep/17 ]

Also, what version of XCode do you have installed?

Comment by Andrew Morrow (Inactive) [ 22/Sep/17 ]

Thank you for filing a detailed ticket. I'm going to attempt to reproduce the issue with the code you provided. From a quick look I don't see anything wrong with it though. Meanwhile, since this is urgent, could you please the compiler invocation for the link step for App? In a typical CMake project I think this would be enabled by adding VERBOSE=1 to your make invocation.

Generated at Wed Feb 07 22:02:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.