2015-12-17 116 views
0

我試圖建立與SCons的一個完整的編譯環境,我碰到這個問題來了:如何使用SCons編譯兩個不同的編譯器?

我的項目可以通過兩種不同的編譯器(C或CPP編譯器)和相同的鏈接生成的目標文件進行編譯連接。

因此,我需要知道如何從鏈接部分拆分編譯部分。

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

這可能使用相同的環境嗎?

回答

1

下面是一個最小的工作示例,用於演示您描述的功能。

>> 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. 
+0

我可以用SConscript做這個嗎? (在編譯期間,某些文件可能有不同的編譯器選項)(AddOption假定我將使用該編譯器調用整個編譯) –

+0

是的,這也適用於SConscript。嘗試重命名SConscript上面的SConscript,並創建一個只包含「SConscript('SConscript')」而不帶引號的文件SConstruct。它的工作原理是一樣的。 SCons很棒,無論你想要完成什麼,都可能有5種或更多種不同的方式來完成它。也就是說,我建議你創建一個具有通用構建設置的默認環境,並將其傳遞給所有的SConscript,每個環境都有一個克隆和專門化。 –

+0

也可以在每個文件編譯後執行一些操作? –

0

創建兩個環境,每個編譯器一個,在必要時使用。 然後使用任何環境來連接來自任一環境的對象。