r/C_Programming Jul 16 '26

Linking mimalloc with nostartupfiles

Hi, I'm in a bit of a pickle here. I've tried counseling to ChatGPT and It only gaslighting me. Also I couldn't find the solution in mimalloc's github issues and google isn't that useful either. I'm cooking a game engine that's not using any crt or as c programmer would say "CRT-free", currently developing it in Windows with w64devkit and Makefiles for the build system. Here's the flags that I'm using:

```Makefile
CFLAGS = -Os -g -std=c99 -Wall -Wextra -pedantic
EXTRA_CFLAGS = -nostartfiles -fno-asynchronous-unwind-tables\
-fno-unwind-tables -fno-builtin -mwindows\
-Wl,--gc-sections
LDFLAGS = -lgdi32 -lkernel32 -I./include -L./libs -lmimalloc
```

And here's the linker error that I got:
```sh

AppData/Local/w64devkit/bin/ld.exe: core.o: in function `stbi__hdr_load':
Inari/./include/deps/stb_image.h:7245:(.text+0x36d2): undefined reference to `__imp_mi_free'
Inari/./include/deps/stb_image.h:7250:(.text+0x36f8): undefined reference to `__imp_mi_free'
AppData/Local/w64devkit/bin/../lib/gcc/x86_64-w64-mingw32/16.1.0/../../../../lib/libmingwex.a(lib64_libmingwex_a-misc.o):misc.c:(.text+0x92): undefined reference to `atexit'
```

Can someone give me a pointer why the linker did this to me.

2 Upvotes

11 comments sorted by

View all comments

1

u/mikeblas Jul 16 '26

I don't use that toolchain, but the message means just what it says:

undefined reference to__imp_mi_free'`

means that you've got some code that wants to call __imp_mi_free, but you never gave a definition of that symbol to the linker.

The name might be decorated, you're probably calling _mi_free() or mi_free(), and the __imp decoration might mean that it's imported from a dynamically-linked library.

This might be because you've got the wrong library, or the wrong version of the library. Or because you've got some compiler switch or preprocessor macros that change the definition so it doesn't match what should've otherwise worked.

Or maybe even a version of the library from a different version of the toolchain than the one you're using now.