mrgloom
mrgloom

Reputation: 21602

"undefined reference" errors when trying to use address sanitizer with GCC

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.

Upvotes: 87

Views: 132576

Answers (4)

Alvov1
Alvov1

Reputation: 307

In addition to other answers. I had the same problem while using CMake on Ubuntu 22.04 x Clang with:

target_compile_options(Foo PRIVATE -fsanitize=address)
target_link_options(Foo PRIVATE -fsanitize=address)

and the solution for me was to switch from PRIVATE options inclusion to PUBLIC:

target_compile_options(Foo PUBLIC -fsanitize=address)
target_link_options(Foo PUBLIC -fsanitize=address)

Upvotes: 1

Smeeheey
Smeeheey

Reputation: 10316

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

Upvotes: 12

yugr
yugr

Reputation: 21878

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").

Upvotes: 173

Jonny
Jonny

Reputation: 320

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

dnf install libasan libasan-static

Upvotes: 14

Related Questions