Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt and Mongocxx linker error



  • Hi everyone,

    I'm working on my project which uses mongoDB. I've installed mongocxx according to the official guide and it works when I'm compiling the code from command line. Now I'd like to use it in my Qt project, I've added all necessary libraries in .pro file but I can't compile my code because of linker error. The code is the simplest:

    #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
    ...
    

    But, unfortunately, there is a linker's error:

    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)
    When executing step "Make"
    

    the make's string:

    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 
    

    I've already found a decision for such problem in different topics, e.g. http://widequestion.com/question/msgasserted-linker-errors-on-mongo-c-drivers-on-macosx-10-10-3-yosemite/
    but it'n't working in my case. My OS X is 10.11.
    Help me, please, to resolve this problem.


  • Lifetime Qt Champion

    Hi,

    Why are you modifying QMAKE_CXXFLAGS ?

    INCLUDEPATH and LIBS are the correct variables to modify.



  • Hi,
    I'm not using it because the libraries aren't included if I do it.
    According to the mongocxx official manual, I have to use the following line to compile my code:

    c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
    

    or

    c++ --std=c++11 test.cpp -o test \
      -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
    

    If I use LIBS, I can see an error "file not found" and there isn't any mongocxx argument at the qmake line


  • Lifetime Qt Champion

    These libraries are in /usr/local/lib which is not in standard search path so you have to make the path known to qmake.

    LIBS += \ 
        -L/usr/local/lib \ 
        -lmongocxx \
        -lother_needed_libraries
    


  • Hmm, I'm stupid... thank you very much, it was the solution


  • Lifetime Qt Champion

    You're welcome !

    Since you have it working now please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)


Log in to reply