如何使用 SCons 使用两个不同的编译器进行编译?

How to compile with two different compilers using SCons?

我正在尝试使用 SCons 设置完整的构建环境,但遇到了这个问题:

我的项目可以用两个不同的编译器(c 或 cpp 编译器)编译,生成的目标文件用同一个链接器链接。

因此,我需要知道如何将编译部分与链接部分分开。

此外,有些情况下我只需要 .o 文件,所以我想避免链接。

是否可以使用相同的环境?

创建两个环境,每个编译器一个,必要时使用。 然后使用您需要的任何环境从任一环境链接对象。

这是一个最小的工作示例,用于演示您描述的功能。

>> tree
.
├── main.cpp
└── SConstruct

0 directories, 2 files

>> cat main.cpp
#include <iostream>
int main() { std::cout << "Hello World" << std::endl; }

>> cat SConstruct 
# Create Build Environment
env = Environment()

# Create Option for Toolchain
AddOption('--toolchain', dest='toolchain', choices=['gnu', 'clang'],
          default='gnu', help='Toolchain Specification')
env.Replace(TOOLCHAIN=GetOption('toolchain'))
if env['TOOLCHAIN'] == 'clang':
    env.Replace(CXX='clang++')

# Build Object File
obj = env.Object('main.cpp')

# Conditionally Build Executable
AddOption('--build-exe', action='store_true', dest='build_exe', default=False,
          help='Build Executable')
if GetOption('build_exe'):
    env.Program(obj)

>> scons --version
SCons by Steven Knight et al.:
    script: v2.3.6.rel_2.3.5:3347:d31d5a4e74b6[MODIFIED], 2015/07/31 14:36:10, by bdbaddog on hpmicrodog
    engine: v2.3.6.rel_2.3.5:3347:d31d5a4e74b6[MODIFIED], 2015/07/31 14:36:10, by bdbaddog on hpmicrodog
    engine path: ['/usr/lib/scons/SCons']
Copyright (c) 2001 - 2015 The SCons Foundation

>> scons --help
scons: Reading SConscript files ...
scons: done reading SConscript files.
usage: scons [OPTION] [TARGET] ...

SCons Options:
  -b, -d, -e, -m, -S, -t, -w, --environment-overrides, --no-keep-going,
  --no-print-directory, --print-directory, --stop, --touch
                              Ignored for compatibility.
  -c, --clean, --remove       Remove specified targets and dependencies.
  -C DIR, --directory=DIR     Change to DIR before doing anything.
  --cache-debug=FILE          Print CacheDir debug info to FILE.
  --cache-disable, --no-cache
                              Do not retrieve built targets from CacheDir.
  --cache-force, --cache-populate
                              Copy already-built targets into the CacheDir.
  --cache-readonly            Do not update CacheDir with built targets.
  --cache-show                Print build actions for files from CacheDir.
  --config=MODE               Controls Configure subsystem: auto, force,
                                cache.
  -D                          Search up directory tree for SConstruct,
                                build all Default() targets.
  --debug=TYPE                Print various types of debugging information:
                                count, duplicate, explain, findlibs, includes,
                                memoizer, memory, objects, pdb, prepare,
                                presub, stacktrace, time.
  --diskcheck=TYPE            Enable specific on-disk checks.
  --duplicate=DUPLICATE       Set the preferred duplication methods. Must be
                                one of hard-soft-copy, soft-hard-copy,
                                hard-copy, soft-copy, copy
  -f FILE, --file=FILE, --makefile=FILE, --sconstruct=FILE
                              Read FILE as the top-level SConstruct file.
  -h, --help                  Print defined help message, or this one.
  -H, --help-options          Print this message and exit.
  -i, --ignore-errors         Ignore errors from build actions.
  -I DIR, --include-dir=DIR   Search DIR for imported Python modules.
  --implicit-cache            Cache implicit dependencies
  --implicit-deps-changed     Ignore cached implicit dependencies.
  --implicit-deps-unchanged   Ignore changes in implicit dependencies.
  --interact, --interactive   Run in interactive mode.
  -j N, --jobs=N              Allow N jobs at once.
  -k, --keep-going            Keep going when a target can't be made.
  --max-drift=N               Set maximum system clock drift to N seconds.
  --md5-chunksize=N           Set chunk-size for MD5 signature computation to
                                N kilobytes.
  -n, --no-exec, --just-print, --dry-run, --recon
                              Don't build; just print commands.
  --no-site-dir               Don't search or use the usual site_scons dir.
  --profile=FILE              Profile SCons and put results in FILE.
  -q, --question              Don't build; exit status says if up to date.
  -Q                          Suppress "Reading/Building" progress messages.
  --random                    Build dependencies in random order.
  -s, --silent, --quiet       Don't print commands.
  --site-dir=DIR              Use DIR instead of the usual site_scons dir.
  --stack-size=N              Set the stack size of the threads used to run
                                jobs to N kilobytes.
  --taskmastertrace=FILE      Trace Node evaluation to FILE.
  --tree=OPTIONS              Print a dependency tree in various formats: all,
                                derived, prune, status.
  -u, --up, --search-up       Search up directory tree for SConstruct,
                                build targets at or below current directory.
  -U                          Search up directory tree for SConstruct,
                                build Default() targets from local SConscript.
  -v, --version               Print the SCons version number and exit.
  --warn=WARNING-SPEC, --warning=WARNING-SPEC
                              Enable or disable warnings.
  -Y REPOSITORY, --repository=REPOSITORY, --srcdir=REPOSITORY
                              Search REPOSITORY for source and target files.

Local Options:
  --toolchain=TOOLCHAIN       Toolchain Specification
  --build-exe                 Build Executable

>> scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o main.o -c main.cpp
scons: done building targets.

>> tree
.
├── main.cpp
├── main.o
└── SConstruct

0 directories, 3 files

>> scons --build-exe
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o main main.o
scons: done building targets.

>> tree
.
├── main
├── main.cpp
├── main.o
└── SConstruct

0 directories, 4 files

>> scons --toolchain=clang --build-exe
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
clang++ -o main.o -c main.cpp
clang++ -o main main.o
scons: done building targets.