使用scons扫描构建使用gcc而不是clang

时间:2017-04-27 19:36:43

标签: scons clang++ clang-static-analyzer

我试图让scan-build使用clang而不是gcc进行编译,但出于某种原因scons总是使用gcc。

如果我执行scons,我就明白了:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
/usr/bin/clang++ -o src/game.o -c -std=c++1z -pedantic -Wall -Wextra -Werror -O3 src/game.cpp
src/game.cpp:13:62: error: unknown type name 'a'
constexpr std::array<piece_t, game::pieces_num> game::pieces;a
                                                             ^
src/game.cpp:15:7: error: constructor cannot have a return type
game::game() noexcept {
      ^~~~
src/game.cpp:16:5: error: use of undeclared identifier 'regen_pieces'
    regen_pieces();
    ^
3 errors generated.
scons: *** [src/game.o] Error 1
scons: building terminated because of errors.

如您所见,它正确使用clang++代替g++。我使用this问题中的答案实现了这一点。

但是,如果我执行scan-build scons,我会得到这个:

scan-build: Using '/usr/bin/clang-4.0' for static analysis
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
/usr/bin/../lib/clang/c++-analyzer -o src/game.o -c -std=c++1z -pedantic -Wall -Wextra -Werror -O3 src/game.cpp
src/game.cpp:13:62: error: 'a' does not name a type
 constexpr std::array<piece_t, game::pieces_num> game::pieces;a
                                                              ^
scons: *** [src/game.o] Error 1
scons: building terminated because of errors.
scan-build: Removing directory '/tmp/scan-build-2017-04-27-213151-27247-1' because it contains no reports.
scan-build: No bugs found.

由于错误消息不同,scan-build使用gcc而不是clang。

以下是我的环境变量的输出:

$ printenv | grep clang
CC=/usr/bin/clang
CXX=/usr/bin/clang++

我的SConstruct文件:

import os
env = Environment()
env["CC"] = os.getenv("CC") or env["CC"]
env["CXX"] = os.getenv("CXX") or env["CXX"]
env["ENV"].update(x for x in os.environ.items() if x[0].startswith("CCC_"))
env["ENV"]["TERM"] = os.environ["TERM"]
env.Append(CXXFLAGS = "-std=c++1z -pedantic -Wall -Wextra -Werror -O3")
env.Append(LIBS = ["pthread"])
env.Program("game", ["src/game.cpp", "src/main.cpp"])

所以,我的问题是如何让scan-build使用clang?

1 个答案:

答案 0 :(得分:2)

如果您没有指定编译器,scan-build读取环境变量,而是猜测。在你的情况下,它猜测合适的编译器是gcc。

scan-build的开发人员正在尝试解决此问题,但与此同时,您必须在构建时明确指定编译器:

$ scan-build --use-c++=clang++ scons