Cannot build C project using mimalloc on mac m1
I have built a simple C project using mimalloc on my mac m1, but when I try to build it, it throw an error
Undefined symbols for architecture arm64:
"std::bad_alloc::bad_alloc()", referenced from:
mi_try_new_handler(bool) in libmimalloc-debug.a[2](alloc.c.o)
"std::bad_alloc::~bad_alloc()", referenced from:
mi_try_new_handler(bool) in libmimalloc-debug.a[2](alloc.c.o)
"std::get_new_handler()", referenced from:
mi_try_new_handler(bool) in libmimalloc-debug.a[2](alloc.c.o)
"std::terminate()", referenced from:
___clang_call_terminate in libmimalloc-debug.a[2](alloc.c.o)
"typeinfo for std::bad_alloc", referenced from:
mi_try_new_handler(bool) in libmimalloc-debug.a[2](alloc.c.o)
"___cxa_allocate_exception", referenced from:
mi_try_new_handler(bool) in libmimalloc-debug.a[2](alloc.c.o)
"___cxa_begin_catch", referenced from:
___clang_call_terminate in libmimalloc-debug.a[2](alloc.c.o)
"___cxa_throw", referenced from:
mi_try_new_handler(bool) in libmimalloc-debug.a[2](alloc.c.o)
"___gxx_personality_v0", referenced from:
/Users/phuc/.vcpkg-clion/vcpkg/installed/arm64-osx/debug/lib/libmimalloc-debug.a[2](alloc.c.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
I use vcpkg to install mimalloc and integrate with my IDE is Clion, this is my .c file and CMakeLists.txt: test.c:
#include <stdio.h>
#include <mimalloc.h>
int main(void) {
int n;
scanf("%d", &n);
int *a = mi_malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
a[i] = i;
}
for (int i = 0; i < n - 1; i++) {
a[i] *= 2;
}
for (int i = 0; i < n - 1; i++) {
printf("%d ", a[i]);
}
mi_free(a);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 4.0)
project(test C)
set(CMAKE_C_STANDARD 23)
set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Target macOS architectures")
add_executable(test main.c)
find_package(mimalloc CONFIG REQUIRED)
target_link_libraries(test PRIVATE $<IF:$<TARGET_EXISTS:mimalloc-static>,mimalloc-static,mimalloc>)
Please help me.
You only enable C in your project command. But libmimalloc-debug.a has C++ linkage, so you should also add CXX.
Thanks !
FTR the vcpkg port explicitly chooses to build mimalloc with C++ linkage (MI_USE_CXX=ON).
Ohh, how I make vcpkg port explicitly chooses to build mimalloc with C ?
Ohh, how I make vcpkg port explicitly chooses to build mimalloc with C ?
You could try to override cmake options, from a custom triplet file, but it may just break things.
This is a kind of choice which isn't easy (or feasible) to expose in the vcpkg universe. Things can break the the way you observed: Some package starts asking for mimalloc with C++, and then projects start failing.
I don't think the issue belongs here (mimalloc), and it probably won't be changed in vcpkg.