如何在CMake中设置ASAN_OPTIONS环境变量?

Ash*_*can 6 cmake address-sanitizer

据我了解,要ASAN_OPTIONS与 clang 一起使用,ASAN_OPTIONS必须在编译之前设置环境变量。

如何在 CMake 脚本中执行此操作而不添加包装器脚本?

仅当使用 clang 编译时,我才需要禁用某个特定测试项目的 ODR 违规检查。所以在CMakeLists.txt文件中我有:

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  # Clange reports ODR Violation errors in mbedtls/library/certs.c.  Need to disable this check.
  set(ENV{ASAN_OPTIONS} detect_odr_violation=0)
endif()
Run Code Online (Sandbox Code Playgroud)

但运行后cmake,如果我输入echo $ASAN_OPTIONS,则未设置。

运行后cmake,如果我输入:

export ASAN_OPTIONS=detect_odr_violation=0
make
Run Code Online (Sandbox Code Playgroud)

一切都很好。

是否可以cmake设置一个环境变量,以便它在cmake运行后仍然存在?抱歉我对环境的了解有限!

yug*_*ugr 10

据我了解,要将 ASAN_OPTIONS 与 clang 一起使用,必须在编译之前设置 ASAN_OPTIONS 环境变量。

不是真的,ASAN_OPTIONS不会以任何方式影响编译。相反,它控制编译代码的行为,因此需要在运行测试之前设置(和导出)。

在您的情况下,使用不同的控制机制会更容易:__asan_default_options 回调

#ifndef __has_feature
// GCC does not have __has_feature...
#define __has_feature(feature) 0
#endif

#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#ifdef __cplusplus
extern "C"
#endif
const char *__asan_default_options() {
  // Clang reports ODR Violation errors in mbedtls/library/certs.c.
  // NEED TO REPORT THIS ISSUE
  return "detect_odr_violation=0";
}
#endif
Run Code Online (Sandbox Code Playgroud)