72

I'm trying to build my project with

g++ -O0 -g -fsanitize=address -fno-omit-frame-pointer

but get lots of errors like:

/home/user/libs/opencv/include/opencv2/core/mat.hpp:715: undefined reference to `__asan_report_load8'

How to compile project with AddressSanitize support?

My gcc version is 4.8.4.

jww
  • 90,984
  • 81
  • 374
  • 818
mrgloom
  • 17,637
  • 28
  • 146
  • 263
  • 6
    That's not the complete compilation line, since you don't have the file name. Did you compile/link in separate steps? You also forgot to mention the version of gcc you are using... – Marc Glisse Jun 22 '16 at 14:37
  • 2
    Please, set the correct answer @yugr one since the one you marked is not really correct. – ceztko Dec 10 '19 at 20:37

3 Answers3

147

You need to add -fsanitize=address to compiler flags (both CFLAGS and CXXFLAGS) and linker flags (LDFLAGS). You've probably added it to your compiler flags only.

Note that using explicit -lasan option has been widely discouraged by ASan developers (e.g. here) as it misses some other important linker flags. The only recommended way to link is to use -fsanitize=address.

As a side note, for more aggressive verification flags check Asan FAQ (look for "more aggressive diagnostics").

yugr
  • 16,844
  • 3
  • 43
  • 82
  • If I skip the `-lasan`, Im getting `undefined reference to __asan_...` – HeinrichStack Feb 12 '18 at 10:58
  • 1
    @HeinrichStack It's hard to comment without repro. Note that `-fsanitize=address` is equivalent to `-lasan` + some other stuff. – yugr Feb 12 '18 at 14:03
  • 2
    When using `-Wl,--no-undefined` together with Clang, one must also add `-shared-libasan` to linker flags. It's also part of official FAQ, but just in case, it's also nice to have this information here. – Dmitry Kochkin Oct 01 '19 at 22:41
  • @DmitryKochkin Choosing static/shared runtime libraries is a can full of worms (default choice is different between LLVM and GCC, `-static-libasan` does no really work in GCC, shared/static runtimes work differently depending on whether main app is sanitized or not, etc.). In general I suggest to stick with default choice made by your compiler when possible and only switch to `-shared-libasan` in edge cases (which are sanitizing only one library and/or using `-Wl,--no-undefined`). Here's a link to [another answer](https://stackoverflow.com/a/47022141/2170527) which provides gory details. – yugr Nov 22 '19 at 10:07
  • @yugr yes, I said it should be used only with `-Wl,--no-undefined`. The linking in C++ is a can of worms just by itself, you are doomed with this language choice anyway. And in your another answer you recommend the same for Clang. – Dmitry Kochkin Nov 28 '19 at 10:17
  • Please emphasize "BOTH" in "to both compiler flags (CFLAGS, CXXFLAGS)" . It does not work when you do it only for one of them!! – U. W. Jun 26 '20 at 13:56
  • @U.W. "both" was actually meant to mean "both compiler and linker flags" but I agree that this might have been unclear. Fixed. – yugr Jun 26 '20 at 15:58
12

Make sure you have libasan installed. For example, in Fedora:

dnf install libasan libasan-static

Jonny
  • 181
  • 1
  • 4
  • 1
    Indeed this may cause similar error when applying Asan to a single shared library and using `-static-libasan`. Otherwise `gcc` will emit distinct error messages (`libasan_preinit.o: No such file or directory` or `cannot find -lasan`). On Ubuntu `libasan` is installed by default with `gcc`. – yugr Aug 03 '18 at 07:39
3

You need to add the switch -lasan -fsanitize=address to your both your compile and link command line to link the correct library.

Note: the original answer -lasan is outdated and should not be used, as per comments

Smeeheey
  • 9,582
  • 18
  • 37
  • This neither works on WSL nor on windows. With all versions of libasan installed. – CodeMonkey Feb 14 '19 at 16:13
  • 2
    In my experience when this is necessary gcc is installed incorrectly – Cruz Jean Apr 04 '19 at 20:13
  • 17
    This should not be the accepted answer. Below answer from @yugr is correct. Just adding this comment so people don't just stop reading after the accepted answer. – hko Nov 05 '19 at 23:32
  • 2
    You should *not* add `-lasan` yourself. You should use the compiler to drive link. The compiler will add the correct libraries when `-fsanitize=address` is present. – jww Nov 16 '19 at 04:16
  • 1
    Also confirming this is not the correct way to enable address sanitizer. – ceztko Dec 10 '19 at 20:38