16

I'm using mingw-w64 (gcc 7.3.0) and when I compile any C++ program using the following command:

g++ file.cpp -fsanitize=undefined

I get the following error:

...mingw32/bin/ld.exe: cannot find -lubsan

I'm able to successfully compile and run my programs if I remove the -fsanitize=undefined flag, though. After some research I found out that this means the library ubsan (Undefined Behavior Sanitizer) is missing, but I couldn't find anything about the library. How do I fix this?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
muzaffarahmed75
  • 170
  • 1
  • 9

1 Answers1

2

This is well known issue with mingw see for instance this msys github issue. No proper solution known but there are several WAs.

  1. Install WSL, ubuntu over WSL and you will have ubsan inside it
  2. Build GCC under Windows from source enabling sanitizers build. They are present in GCC sources they are just not here in mingw.
  3. Use -fsanitize=undefined -fsanitize-undefined-trap-on-error to just not use libubsan rich logging capabilities but get it trap on undefined instruction.

Hope one of this helps.

Konstantin Vladimirov
  • 6,791
  • 1
  • 27
  • 36