I have an issue with linking libmongocxx in my project.
I am using CMAKE to link the library and I am running Ubuntu 17.10.
Here is my CMakeLists.txt :
cmake_minimum_required(VERSION 3.9) project(web_service_cpp) SET(CMAKE_CXX_STANDARD 14) set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin) set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib) find_package(Boost REQUIRED) find_package(Hana REQUIRED) find_package(libmongocxx REQUIRED) find_package(libbsoncxx REQUIRED) add_library(libmongocxx INTERFACE IMPORTED) set_property(TARGET libmongocxx PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${libmongocxx_INCLUDE_DIR}) add_library(boost INTERFACE IMPORTED) set_property(TARGET boost PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${Boost_INCLUDE_DIR}) include_directories(${LIBMONGOCXX_INCLUDE_DIR}) include_directories(${LIBBSONCXX_INCLUDE_DIR}) include_directories("/usr/local/include/mongocxx/v_noabi") include_directories("/usr/local/include/bsoncxx/v_noabi") include_directories("/usr/local/include/libmongoc-1.0") include_directories("/usr/local/include/libbson-1.0") include_directories("/usr/local/lib") include_directories("${PROJECT_SOURCE_DIR}") set(SOURCE_FILES src/main.cpp src/cMqttService.cpp src/cMongoDB.cpp src/cRepository.cpp) set(HEADER_FILES include/cRepository.h include/cMqttService.h include/cMongoDB.h include/main.h) add_executable(web_service_cpp ${HEADER_FILES} ${SOURCE_FILES}) target_link_libraries(web_service_cpp ${LIBMONGOCXX_LIBRARIES}) target_link_libraries(web_service_cpp ${LIBBSONXX_LiBRARIES})
I find it weird that I am getting this error since it use to work on macOS :
[ 20%] Building CXX object CMakeFiles/web_service_cpp.dir/src/main.cpp.o [ 40%] Building CXX object CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o [ 60%] Building CXX object CMakeFiles/web_service_cpp.dir/src/cMongoDB.cpp.o [ 80%] Building CXX object CMakeFiles/web_service_cpp.dir/src/cRepository.cpp.o [100%] Linking CXX executable bin/bin/web_service_cpp CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o: In function `cMqttService::initCommunication()': cMqttService.cpp:(.text+0x1da): undefined reference to `mqtt::async_client::async_client(std::__cxx11::basic_string, std::allocator > const&, std::__cxx11::basic_string, std::allocator > const&, mqtt::iclient_persistence*)' cMqttService.cpp:(.text+0x225): undefined reference to `mqtt::connect_options::connect_options()' cMqttService.cpp:(.text+0x266): undefined reference to `mqtt::connect_options::connect_options(mqtt::connect_options const&)' cMqttService.cpp:(.text+0x286): undefined reference to `mqtt::async_client::connect(mqtt::connect_options)' cMqttService.cpp:(.text+0x3ca): undefined reference to `mqtt::async_client::publish(std::shared_ptr)' cMqttService.cpp:(.text+0x464): undefined reference to `mqtt::async_client::publish(mqtt::buffer_ref, void const*, unsigned long, int, bool)' cMqttService.cpp:(.text+0x4ea): undefined reference to `mqtt::async_client::~async_client()' cMqttService.cpp:(.text+0x63f): undefined reference to `mqtt::async_client::~async_client()' CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o: In function `mqtt::async_client::disconnect()': cMqttService.cpp:(.text._ZN4mqtt12async_client10disconnectEv[_ZN4mqtt12async_client10disconnectEv]+0x36): undefined reference to `mqtt::disconnect_options::disconnect_options()' CMakeFiles/web_service_cpp.dir/src/cMqttService.cpp.o: In function `void __gnu_cxx::new_allocator::construct, mqtt::buffer_ref, int&, bool&>(mqtt::message*, mqtt::buffer_ref&&, mqtt::buffer_ref&&, int&, bool&)': cMqttService.cpp:(.text._ZN9__gnu_cxx13new_allocatorIN4mqtt7messageEE9constructIS2_JNS1_10buffer_refIcEES6_RiRbEEEvPT_DpOT0_[_ZN9__gnu_cxx13new_allocatorIN4mqtt7messageEE9constructIS2_JNS1_10buffer_refIcEES6_RiRbEEEvPT_DpOT0_]+0xdc): undefined reference to `mqtt::message::message(mqtt::buffer_ref, mqtt::buffer_ref, int, bool)' CMakeFiles/web_service_cpp.dir/src/cMongoDB.cpp.o: In function `cMongoDB::cMongoDB(std::__cxx11::basic_string, std::allocator >, short)': cMongoDB.cpp:(.text+0x63): undefined reference to `mongocxx::v_noabi::instance::instance()' cMongoDB.cpp:(.text+0x7c): undefined reference to `mongocxx::v_noabi::uri::k_default_uri[abi:cxx11]' cMongoDB.cpp:(.text+0x96): undefined reference to `mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)' cMongoDB.cpp:(.text+0xb8): undefined reference to `mongocxx::v_noabi::client::client()' cMongoDB.cpp:(.text+0x190): undefined reference to `mongocxx::v_noabi::uri::uri(bsoncxx::v_noabi::string::view_or_value)' cMongoDB.cpp:(.text+0x1ad): undefined reference to `mongocxx::v_noabi::uri::operator=(mongocxx::v_noabi::uri&&)' cMongoDB.cpp:(.text+0x1bc): undefined reference to `mongocxx::v_noabi::uri::~uri()' cMongoDB.cpp:(.text+0x215): undefined reference to `mongocxx::v_noabi::client::client(mongocxx::v_noabi::uri const&, mongocxx::v_noabi::options::client const&)' cMongoDB.cpp:(.text+0x232): undefined reference to `mongocxx::v_noabi::client::operator=(mongocxx::v_noabi::client&&)' cMongoDB.cpp:(.text+0x241): undefined reference to `mongocxx::v_noabi::client::~client()' cMongoDB.cpp:(.text+0x2e3): undefined reference to `mongocxx::v_noabi::client::~client()' cMongoDB.cpp:(.text+0x2f6): undefined reference to `mongocxx::v_noabi::uri::~uri()' cMongoDB.cpp:(.text+0x309): undefined reference to `mongocxx::v_noabi::instance::~instance()' collect2: error: ld returned 1 exit status CMakeFiles/web_service_cpp.dir/build.make:172: recipe for target 'bin/bin/web_service_cpp' failed make[2]: *** [bin/bin/web_service_cpp] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/web_service_cpp.dir/all' failed make[1]: *** [CMakeFiles/web_service_cpp.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2
I am open to suggestions! Thank you for your help in advance.
LIBMONGOXX_LIBRARIES
is a typo: it should beLIBMONGOCXX_LIBRARIES
. Also, do never set CMAKE_BINARY_DIR variable: many external and internal CMake functionality treat it as a pointer to the build directory (wherecmake
has been invoked). – Tsyvarev