Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple definition errors when including mimalloc-new-delete.h. #559

Open
h-barthwal opened this issue Mar 11, 2022 · 3 comments
Open

Multiple definition errors when including mimalloc-new-delete.h. #559

h-barthwal opened this issue Mar 11, 2022 · 3 comments

Comments

@h-barthwal
Copy link

h-barthwal commented Mar 11, 2022

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 !

@res2k
Copy link
Contributor

res2k commented Mar 11, 2022

This sounds a lot like #535, have a look there

@h-barthwal
Copy link
Author

h-barthwal commented Mar 11, 2022

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

@yhyu13
Copy link

yhyu13 commented Jan 30, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants