Qt 和 Mongocxx 链接器错误

Qt and Mongocxx linker error

本文关键字:错误 链接 Mongocxx Qt      更新时间:2023-10-16

我正在研究使用mongoDB的项目。我已经根据官方指南安装了mongocxx,当我从命令行编译代码时它可以工作。现在我想在我的Qt项目中使用它,我已经在.pro文件中添加了所有必要的库,但由于链接器错误,我无法编译我的代码。代码是最简单的:

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
void connectToMongo() {
mongocxx::instance abc{};
}

.pro:

CONFIG += c++11
QMAKE_CXXFLAGS += -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 
  -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 
  -L/usr/local/lib -lmongocxx -lbsoncxx
SOURCES += main.cpp
...

但是,不幸的是,存在链接器的错误:

Undefined symbols for architecture x86_64:
  "mongocxx::v_noabi::instance::instance()", referenced from:
      connectToMongo() in main.o
  "mongocxx::v_noabi::instance::~instance()", referenced from:
      connectToMongo() in main.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: *** [hw.app/Contents/MacOS/hw] Error 1
18:38:01: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project hw (kit: Desktop Qt 5.8.0 clang 64bit)

执行步骤"制作"时品牌的字符串:

Starting: "/usr/bin/make" 
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -c -pipe -stdlib=libc++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -mmacosx-version-min=10.12  -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 -L/usr/local/lib -lmongocxx -lbsoncxx -g -std=gnu++1z -Wall -W -fPIC -DQT_QML_DEBUG -DQT_WEBENGINEWIDGETS_LIB -DQT_WEBENGINECORE_LIB -DQT_QUICKCONTROLS2_LIB -DQT_QUICK_LIB -DQT_PRINTSUPPORT_LIB -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_WEBCHANNEL_LIB -DQT_QML_LIB -DQT_NETWORK_LIB -DQT_POSITIONING_LIB -DQT_CORE_LIB -I../hw -I. -I../Qt/5.8/clang_64/lib/QtWebEngineWidgets.framework/Headers -I../Qt/5.8/clang_64/lib/QtWebEngineCore.framework/Headers -I../Qt/5.8/clang_64/lib/QtQuickControls2.framework/Headers -I../Qt/5.8/clang_64/lib/QtQuick.framework/Headers -I../Qt/5.8/clang_64/lib/QtPrintSupport.framework/Headers -I../Qt/5.8/clang_64/lib/QtWidgets.framework/Headers -I../Qt/5.8/clang_64/lib/QtGui.framework/Headers -I../Qt/5.8/clang_64/lib/QtWebChannel.framework/Headers -I../Qt/5.8/clang_64/lib/QtQml.framework/Headers -I../Qt/5.8/clang_64/lib/QtNetwork.framework/Headers -I../Qt/5.8/clang_64/lib/QtPositioning.framework/Headers -I../Qt/5.8/clang_64/lib/QtCore.framework/Headers -I. -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/AGL.framework/Headers -I../Qt/5.8/clang_64/mkspecs/macx-clang -F/Users/alexey/Qt/5.8/clang_64/lib -o main.o ../hw/main.cpp
clang: warning: -lmongocxx: 'linker' input unused
clang: warning: -lbsoncxx: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -headerpad_max_install_names -stdlib=libc++ -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -mmacosx-version-min=10.12  -Wl,-rpath,/Users/alexey/Qt/5.8/clang_64/lib -o hw.app/Contents/MacOS/hw main.o qrc_qml.o moc_filldatacollections.o   -F/Users/alexey/Qt/5.8/clang_64/lib -framework QtWebEngineWidgets -framework QtWebEngineCore -framework QtQuick -framework QtQml -framework QtNetwork -framework QtCore -framework DiskArbitration -framework IOKit -framework QtGui -framework QtWebChannel -framework QtPositioning -framework QtPrintSupport -framework QtWidgets -framework QtQuickControls2 -framework OpenGL -framework AGL 

我已经在不同的主题中找到了针对此类问题的决定,例如 http://widequestion.com/question/msgasserted-linker-errors-on-mongo-c-drivers-on-macosx-10-10-3-yosemite/

但它在我的情况下不起作用。我的 OS X 是 10.11。请帮帮我解决这个问题。

clang: warning: -lmongocxx: 'linker' input unused
clang: warning: -lbsoncxx: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/local/lib'

您似乎已将链接器标志设置为 cxxflags,因此它们在构建 main.o 时(当它们未使用时(应用,并且在链接期间未应用以构建可执行文件。

我没有使用过Qt,但快速搜索表明,也许这些应该在LIBS上设置。 请参阅文档以声明其他库。

相关文章: