windowsclangaddress-sanitizerlld

Clang with Address Sanitizer on Windows fails at link time


I have the following setup:

The second command fails at link time:

cmake -S . -B build-windows -D CMAKE_C_COMPILER=clang -G "Unix Makefiles"
cmake --build build-windows

The output contains the following 4 warnings and 1 error:

lld-link: warning: CMakeFiles/asan-test.dir/asan-test.c.obj: locally defined symbol imported: malloc (defined in clang_rt.asan-x86_64.lib(asan_malloc_win.cpp.obj)) [LNK4217]
lld-link: warning: CMakeFiles/asan-test.dir/asan-test.c.obj: locally defined symbol imported: free (defined in clang_rt.asan-x86_64.lib(asan_malloc_win.cpp.obj)) [LNK4217]
lld-link: warning: CMakeFiles/asan-test.dir/asan-test.c.obj: locally defined symbol imported: fclose (defined in libucrt.lib(fclose.obj)) [LNK4217]
lld-link: warning: CMakeFiles/asan-test.dir/asan-test.c.obj: locally defined symbol imported: __stdio_common_vsprintf (defined in libucrt.lib(output.obj)) [LNK4217]
lld-link: error: undefined symbol: __declspec(dllimport) fopen
>>> referenced by <...>\asan-test\asan-test.c:8
>>>               CMakeFiles/asan-test.dir/asan-test.c.obj:(main)

I'm not sure how to approach this problem. I couldn't find anything similar, or maybe I didn't look in the right places.


Solution

  • I got it working. I came across this page from the Windows port of Address Sanitizer, and the takeaway is that the Windows port...

    I changed the setup as follows: