2017-09-22 52 views
1

我目前正在開發一個C++應用程序,它需要使用mongo-cxx-driver來訪問MongoDB實例。我嘗試了幾種安裝方法,並且每次都遇到相同的鏈接器問題。當使用mongo-cxx驅動程序構建C++項目時鏈接錯誤

起初,我試圖安裝mongo-cxx-driversmongod-c-driver詳見這裏: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-drivermongo-c-driver這次安裝通過自制軟件,但遇到了相同的錯誤。

在我的研究中,最關心的StackOverflow帖子是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; 
} 
+0

是不是mongodb是針對libstdC++編譯的,但是您的工具鏈是否與libC++鏈接?例如,如果依賴項是用gcc編譯的,而應用程序是用clang編譯的,則會發生這種情況。 –

回答

1

原來我正在使用CMake的GLOB錯誤。

改變

target_link_libraries(App ${COMMON_LIBRARIES}) 

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

固定的問題。

+1

很高興聽到您啓動並運行 – acm

相關問題