Multiple definition errors when including mimalloc-new-delete.h.
I am trying to override the new/delete operators as suggested in the documentation. I am using vcpkg and added the following statements in my CMakeLists.txt file:
find_package(mimalloc 1.6 REQUIRED)
...
target_link_libraries(my_webserver PUBLIC mimalloc-static)
My program compiles just fine with the above changes but when I include mimalloc-new-delete.h, it starts to give linker errors as follows:
/usr/bin/ld: /vcpkg/installed/x64-linux/share/mimalloc/../../lib/libmimalloc.a(alloc.c.o): in function `operator delete[](void*)':
alloc.c:(.text+0x140): multiple definition of `operator delete[](void*)'; CMakeFiles/my_webserver .dir/service/my_webserver .cpp.o:my_webserver .cpp:(.text+0x10): first defined here
/usr/bin/ld: /vcpkg/installed/x64-linux/share/mimalloc/../../lib/libmimalloc.a(alloc.c.o): in function `operator delete[](void*, std::align_val_t)':
...
How do I avoid these multiple definition errors? Any help is appreciated. Thanks in advance !
This sounds a lot like #535, have a look there
@res2k thanks for the quick response.
MI_OVERRIDE=ON defines all the standard malloc api entry points, and also the C++ new/delete entry points.
I installed mimalloc using vcpkg install mimalloc[override] which would have turned MI_OVERRIDE ON and overridden both malloc and new/delete operators and made #include <mimalloc-new-delete.h> redundant. If so, then I can probably just link to the mimalloc-static and get the desired behavior [i.e. override malloc and new/delete operators] without making any C++ code changes.
Am I understanding it correctly?
As the official doc stated clearly https://microsoft.github.io/mimalloc/using.html
For convience, mimalloc provides mimalloc-new-delete.h which does this for you – just include it in a single(!) source file in your project without linking to the mimalloc's library.
the tricky part is without linking to the mimalloc's library. it actually means no linking to the miamllloc lib with MI_OVERRIDE=1.
You either link to mimalloc with MI_OVERRIDE=0 and with mimalloc-new-delete.h included in a single source.
Or you link to mimalloc with MI_OVERRIDE=1 without considering mimalloc-new-delete.h at all
You can test with MIMALLOC_SHOW_STATS=1 ./a see if there are mimalloc output in the terminal to see if mimalloc overriden takes effect
For instance
a.h
#include <mimalloc.h>
a.cpp
#include <mimalloc-new-delete.h>
...
And then link with mimalloc