Qt World Summit: Submit your Presentation

I am getting build error when Linking QtCore.a with my Memstub file

  • up vote
    down vote
    I have a file

    1. File myStubs.cpp

    #include <stdlib.h>
    #include <new>

    void* qMalloc(size_t sz) {return malloc(sz);}
    void qFree(void* ptr) {free(ptr);}
    void* qRealloc(void* ptr, size_t sz) {return realloc(ptr, sz);}
    I make a static library from this file

    1. g++ -fPIC -c myStubs.cpp -o lib_mylib.o
    2. ar rcs libMyLib.a lib_mylib.o
      In Qt Core we have file qglobal.cpp

    2 ) File is qt-x11-opensource-src-4.3.3/src/corelib/global/qglobal.cpp and same functions as above in this file are

    These functions make it possible to use standard C++ functions with
    a similar name from Qt header files (especially template classes).
    Q_CORE_EXPORT void *qMalloc(size_t size);
    Q_CORE_EXPORT void qFree(void *ptr);
    Q_CORE_EXPORT void *qRealloc(void *ptr, size_t size);
    When I link the static library libMyLib.a and static library of QtCore (libQtCore.a) and QtGui (libQtGui.a) . I am getting following build error

    lib/libQtCore.a(qglobal.o): In function qMalloc(unsigned long)': qglobal.cpp:(.text+0x170): multiple definition ofqMalloc(unsigned long)'
    libMyLib.a(myStubs.o):myStubs.cpp:(.text+0x0): first defined here

    1. If I remove qMalloc , qFree and qRealloc from file myStubs.cpp, I do not get the build error , Is this correct way of solving this problem

    Looking forward for the feedback

  • Moderators

    @Qt-Enthusiast said in I am getting undefined reference when Linking QtCore.a with my Memstub file:

    Is this correct way of solving this problem

    You should state what the problem is. If it's the linker error, then yes, removing the duplicate symbols is the correct way.

  • Lifetime Qt Champion


    You're not getting an undefined error, you're getting a defined multiple time error. AFAIK, if you want to customize qMalloc and friends you have to modify Qt's source code and build it yourself.

  • What is purpose of Stub functions

  • Lifetime Qt Champion

    See here

Log in to reply