Tuesday, February 6, 2024

[SOLVED] Mongo CXX Driver: 'core/optional.hpp' file not found

Issue

Did anybody have any luck using the official MongoDB C++ driver on Mac OS X? I'm using an Apple Silicon Mac and I've tried including both the library installed via brew install mongo-cxx-driver and one built from the release tarball, but both versions give me the same issue when running cmake --build build on my project:

[ 50%] Building CXX object CMakeFiles/server.dir/main.cpp.o
In file included from /Users/trentwt/Development/CPPRestAPI/main.cpp:3:
In file included from /opt/homebrew/include/mongocxx/v_noabi/mongocxx/client.hpp:19:
In file included from /opt/homebrew/include/mongocxx/v_noabi/mongocxx/client_session.hpp:20:
In file included from /opt/homebrew/include/bsoncxx/v_noabi/bsoncxx/document/view.hpp:21:
In file included from /opt/homebrew/include/bsoncxx/v_noabi/bsoncxx/document/element.hpp:20:
/opt/homebrew/include/bsoncxx/v_noabi/bsoncxx/stdx/optional.hpp:21:10: fatal error: 'core/optional.hpp' file not found
#include <core/optional.hpp>
         ^~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/server.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/server.dir/all] Error 2
make: *** [all] Error 2

I assume it wants to use boost/optional.hpp, but it's trying core/optional.hpp instead; I don't know. I'm including my CMakeLists.txt here:

cmake_minimum_required(VERSION 3.27)

project(server)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

file(GLOB sources "${PROJECT_SOURCE_DIR}/*.cpp")

add_executable(${PROJECT_NAME} ${sources})

target_include_directories(${PROJECT_NAME} 
  PUBLIC "/opt/homebrew/include"
  PUBLIC "/opt/homebrew/include/mongocxx/v_noabi/"
  PUBLIC "/opt/homebrew/include/bsoncxx/v_noabi/"
  PUBLIC "${PROJECT_SOURCE_DIR}/dependencies/Crow/include/"
)

Any help is appreciated.


Solution

So as it turns out, it wasn't referencing boost; it was referencing a package mnmlstc/core I cloned the repository for that library, placed it into my dependencies folder, and added it to target_include_directories in the CMakeLists.txt, and it seemed to do the trick. I also had to reference the libraries for the linker.

On the off-chance this may help somebody, I'll paste the CMakeLists.txt file here:

cmake_minimum_required(VERSION 3.27)

project(server)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

file(GLOB SOURCES "${PROJECT_SOURCE_DIR}/*.cpp")

add_executable(${PROJECT_NAME} ${SOURCES})

find_package(Boost REQUIRED)

target_include_directories(${PROJECT_NAME} 
  PUBLIC ${Boost_INCLUDE_DIR}
  PUBLIC "${PROJECT_SOURCE_DIR}/dependencies/mongo-cxx/include/mongocxx/v_noabi"
  PUBLIC "${PROJECT_SOURCE_DIR}/dependencies/mongo-cxx/include/bsoncxx/v_noabi"
  PUBLIC "${PROJECT_SOURCE_DIR}/dependencies/core/include"
  PUBLIC "${PROJECT_SOURCE_DIR}/dependencies/Crow/include"
)

target_link_libraries(${PROJECT_NAME} 
  PRIVATE "${PROJECT_SOURCE_DIR}/dependencies/mongo-cxx/lib/libmongocxx.dylib"
  PRIVATE "${PROJECT_SOURCE_DIR}/dependencies/mongo-cxx/lib/libbsoncxx.dylib"
)

Ideally I would use find_package, but I figure this is not meant to be released.



Answered By - Trentwt
Answer Checked By - Mary Flores (WPSolving Volunteer)