Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. I am getting build error when Linking QtCore.a with my Memstub file
Qt 6.11 is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 3 Posters 2.0k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by Qt Enthusiast
    #1

    up vote
    0
    down vote
    favorite
    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 of qMalloc(unsigned long)'
    libMyLib.a(myStubs.o):myStubs.cpp:(.text+0x0): first defined here
    Questions

    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

    kshegunovK 1 Reply Last reply
    0
    • Q Qt Enthusiast

      up vote
      0
      down vote
      favorite
      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 of qMalloc(unsigned long)'
      libMyLib.a(myStubs.o):myStubs.cpp:(.text+0x0): first defined here
      Questions

      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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

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

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        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.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • Q Offline
          Q Offline
          Qt Enthusiast
          wrote on last edited by
          #4

          What is purpose of Stub functions

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            See here

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved