0
我试图建立与SCons的一个完整的编译环境,我碰到这个问题来了:如何使用SCons编译两个不同的编译器?
我的项目可以通过两种不同的编译器(C或CPP编译器)和相同的链接生成的目标文件进行编译连接。
因此,我需要知道如何从链接部分拆分编译部分。
此外,有些情况下我只需要.o文件,所以我想避免链接。
这可能使用相同的环境吗?
我试图建立与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.
创建两个环境,每个编译器一个,在必要时使用。 然后使用任何环境来连接来自任一环境的对象。
我可以用SConscript做这个吗? (在编译期间,某些文件可能有不同的编译器选项)(AddOption假定我将使用该编译器调用整个编译) –
是的,这也适用于SConscript。尝试重命名SConscript上面的SConscript,并创建一个只包含“SConscript('SConscript')”而不带引号的文件SConstruct。它的工作原理是一样的。 SCons很棒,无论你想要完成什么,都可能有5种或更多种不同的方式来完成它。也就是说,我建议你创建一个具有通用构建设置的默认环境,并将其传递给所有的SConscript,每个环境都有一个克隆和专门化。 –
也可以在每个文件编译后执行一些操作? –