如何在整个项目上运行消毒剂

发布于 2025-01-18 15:03:02 字数 940 浏览 0 评论 0原文

我正在尝试熟悉 ASAN、LSAN 等消毒剂,并从这里获得了很多有用的信息:https://developers.redhat.com/blog/2021/05/05/memory-error-checking-in-c-and-c-comparing-sanitizers-and-valgrind

我能够对特定文件运行各种清理程序,如网站上所示,如下所示:

clang -g -fsanitize=address -fno-omit-frame-pointer -g ../TestFiles/ASAN_TestFile.c
ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out >../Logs/ASAN_C.log 2>&1

这会生成包含发现问题的日志。现在我想扩展它以在使用 cmake 构建项目时运行。这是目前构建它的命令:

cmake -S . -B build
cd build
make

有什么方法可以使用此脚本添加消毒剂,而不必更改 cmakelist.txt 文件?

例如这样的事情:

cmake -S . -B build
cd build
make -fsanitize=address
./a.out >../Logs/ASAN_C.log 2>&1

原因是我希望能够使用不同的消毒剂多次构建项目(因为它们不能一起使用),并在不更改 cmakelist.txt 文件的情况下创建日志(只是希望能够快速测试整个项目的内存问题,而不是对创建的每个文件进行测试)。

I'm trying to get familiar with sanitizers as ASAN, LSAN etc and got a lot of useful information already from here: https://developers.redhat.com/blog/2021/05/05/memory-error-checking-in-c-and-c-comparing-sanitizers-and-valgrind

I am able to run all sort of sanitizers on specific files, as shown on the site, like this:

clang -g -fsanitize=address -fno-omit-frame-pointer -g ../TestFiles/ASAN_TestFile.c
ASAN_SYMBOLIZER_PATH=/usr/local/bin/llvm-symbolizer ./a.out >../Logs/ASAN_C.log 2>&1

which generates a log with found issue. Now I would like to extend this to run upon building the project with cmake. This is the command to build it at the moment:

cmake -S . -B build
cd build
make

Is there any way I can use this script with adding the sanitizers, without having to alter the cmakelist.txt file??

For instance something like this:

cmake -S . -B build
cd build
make -fsanitize=address
./a.out >../Logs/ASAN_C.log 2>&1

The reason is that I want to be able to build the project multiple times with different sanitizers (since they cannot be used together) and have a log created without altering the cmakelist.txt file (just want to be able to quickly test the whole project for memory issues instead of doing it for each file created).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情愿 2025-01-25 15:03:02

您可以在构建配置期间从命令行中添加其他编译器标志:

cmake -D CMAKE_CXX_FLAGS="-fsanitize=address" -D CMAKE_C_FLAGS="-fsanitize=address"  /path/to/CMakeLists.txt

如果您的cmakelists.txt适当配置了上面的配置。如果那不起作用,请尝试将标志添加为环境变量:

cmake -E env CXXFLAGS="-fsanitize=address" CFLAGS="-fsanitize=address"  /path/to/CMakeLists.txt

You can add additional compiler flags from command line during the build configuration:

cmake -D CMAKE_CXX_FLAGS="-fsanitize=address" -D CMAKE_C_FLAGS="-fsanitize=address"  /path/to/CMakeLists.txt

If your CMakeLists.txt is configured properly above should work. If that does not work then try adding flags as environment variable:

cmake -E env CXXFLAGS="-fsanitize=address" CFLAGS="-fsanitize=address"  /path/to/CMakeLists.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文