How to use AddressSanitizer in gcc?

2019-03-09 17:43发布

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.

3条回答
男人必须洒脱
2楼-- · 2019-03-09 17:44

You need to add -fsanitize=address to both compiler flags (CFLAGS, 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").

查看更多
走好不送
3楼-- · 2019-03-09 17:45

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

dnf install libasan libasan-static

查看更多
放荡不羁爱自由
4楼-- · 2019-03-09 17:49

You need to add the switch -lasan to your compile/link command line to link the correct library.

查看更多
登录 后发表回答