83

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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
mrgloom
  • 20,061
  • 36
  • 171
  • 301
  • 7
    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

166

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
  • 19,769
  • 3
  • 51
  • 96
  • 1
    If I skip the `-lasan`, Im getting `undefined reference to __asan_...` – HeinrichStack Feb 12 '18 at 10:58
  • 2
    @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
  • @yugr If I add it to the linker `ld`/`ldd` it throws `unrecognized option `-fsanitize=address'` error. Is the linker supposed to be invoked via `clang`? – User 10482 Jul 05 '23 at 17:11
  • 1
    @User10482 Yes, you are right, this is a gcc/clang option (`LDFLAGS` variable is typically used for "gcc flags used for linking"). You can find which ld/lld flags it expands to by running gcc/clang with `-v` flag. – yugr Jul 06 '23 at 15:53
14

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

dnf install libasan libasan-static

Jonny
  • 301
  • 2
  • 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
10

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,906
  • 23
  • 39
  • 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
  • 19
    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
  • 3
    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