306 words
2 minutes
Sanitizer_is_a_tool_developed_by_Google

link#

C언어에서 sanitizer컴파일 옵션#

  • justfile로 자동화 시킴
# Clang Sanitize(ASan=address / LSan=leak / TSan=thread / MSan=memory / UBSan=undefined (Undefined Behavior)
clang_which := `which clang`
# Files
source := src_dir+"/main.c"

san SAN:
    rm -rf target
    mkdir -p target
    {{clang_which}} -g -fsanitize={{SAN}} -fno-omit-frame-pointer -c {{source}}
    {{clang_which}} -g -fsanitize={{SAN}} *.o 
    mv a.out *.o {{target_dir}}
    {{target_dir}}/a.out

출력된 모습#

$ LSAN_OPTIONS=suppressions=../.lsan.supp
rm -rf target
mkdir -p target
/opt/homebrew/opt/llvm/bin/clang -g -fsanitize=leak -fno-omit-frame-pointer -c ./src/main.c
/opt/homebrew/opt/llvm/bin/clang -g -fsanitize=leak *.o
mv a.out *.o ./target
./target/a.out

Hello, Worl
sizeof(*s) = 1
-----------------------------------------------------
Suppressions used:
  count      bytes template
      3        120 libobjc.A.dylib

Sanitizers - Tools|🔝|#

sanitizers 설명서#

Thread 설명서#


gcc로 하는 방법#

gcc main.c -o main -fsanitize=address -static-libasan -g
  • Or, splitting into separate compiling and linking stages:
gcc -c main.c -fsanitize=address -g
gcc main.o -o main -fsanitize=address -static-libasan

cmake로 자동화 시키기(C23)#

cmake_minimum_required(VERSION 4.0)

get_filename_component(ProjectId ${CMAKE_CURRENT_SOURCE_DIR} NAME)
string(REPLACE " " "_" ProjectId ${ProjectId})
project(${ProjectId} LANGUAGES C)

# Force GCC 15
set(CMAKE_C_COMPILER "/opt/gcc-15/bin/gcc")

# Force Clang 20(macOS)
# set(CMAKE_CXX_COMPILER "/opt/homebrew/opt/llvm/bin/clang")

set(CMAKE_C_STANDARD 23)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)
SET (CMAKE_C_FLAGS_INIT                "-Wall -std=c23")
SET (CMAKE_C_FLAGS_DEBUG_INIT          "-g")
SET (CMAKE_C_FLAGS_MINSIZEREL_INIT     "-Os -DNDEBUG")
SET (CMAKE_C_FLAGS_RELEASE_INIT        "-O3 -DNDEBUG")
SET (CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")

SET (CMAKE_CXX_FLAGS_INIT                "-Wall -std=c++23")
SET (CMAKE_CXX_FLAGS_DEBUG_INIT          "-g")
SET (CMAKE_CXX_FLAGS_MINSIZEREL_INIT     "-Os -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELEASE_INIT        "-O3 -DNDEBUG")
SET (CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g")

# Common compile flags
add_compile_options(
    -pedantic
    -pthread
    -pedantic-errors
    -lm
    -Wall
    -Wextra
    -ggdb
    -g
    -fsanitize=address
    # -std=c23
)

# Main executable with C23 sources
add_executable(${ProjectId}
    src/main.c
    # src/add_sources.c
)

target_link_options(${ProjectId} PRIVATE -pthread -lm -fsanitize=address)

# Output directory
set_target_properties(${ProjectId} PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/target
)
Sanitizer_is_a_tool_developed_by_Google
https://younghakim7.github.io/blog/posts/sanitizer_is_a_tool_developed_by_google/
Author
YoungHa
Published at
2025-12-31