2013-02-28 150 views
5

我想建立一个静态和共享库与SCons使用相同的来源。scons建立静态和共享库

一切工作正常,如果我只是建立一个或另一个,但只要我尝试构建两个,只建立静态库。

我SConscript样子:

cppflags = SP3_env['CPPFLAGS'] 
cppflags += ' -fPIC ' 
SP3_env['CPPFLAGS'] = cppflags 

soLibFile = SP3_env.SharedLibrary(
    target = "sp3", 
    source = sources) 
installedSoFile = SP3_env.Install(SP3_env['SP3_lib_dir'], soLibFile) 

libFile = SP3_env.Library(
    target = "sp3", 
    source = sources) 
installedLibFile = SP3_env.Install(SP3_env['SP3_lib_dir'], libFile) 

我也试过共享对象(源)的SharedLibrary之前(通过从共享对象的回报,而不是源),但它是没有什么不同。如果我在.so之前构建.a,也是如此。

我该如何解决这个问题?

回答

6

当安装目录为或在当前目录下不,使用SCons并不像预期的那样,如SCons Install method docs:

注意评论,但是,在安装文件仍然被认为是一个类型 文件“build”。当您记住SCons的默认 行为是在当前目录中或以下创建文件时,这一点很重要。 如上例所示,如果要在顶级SConstruct文件的目录树以外的目录 中安装文件,则必须指定 的目录(或更高的目录,例如/)为其安装任何内容有:

也就是说,您必须调用SCONS并将安装目录作为目标(您的情况为SP3_env['SP3_lib_dir'])才能执行安装。为了简化这一点,请按照以下方式使用env.Alias()

当您调用SCons时,您至少应该看到静态库和共享库都建立在本地项目目录中。然而,我想象,SCons不会安装它们。下面是我在Ubuntu上提出,工作的例子:

env = Environment() 

sourceFiles = 'ExampleClass.cc' 

sharedLib = env.SharedLibrary(target='example', source=sourceFiles) 
staticLib = env.StaticLibrary(target='example', source=sourceFiles) 

# Notice that installDir is outside of the local project dir 
installDir = '/home/notroot/projects/sandbox' 

sharedInstall = env.Install(installDir, sharedLib) 
staticInstall = env.Install(installDir, staticLib) 

env.Alias('install', installDir) 

如果我执行scons的,没有目标,我得到如下:

# scons 
scons: Reading SConscript files ... 
scons: done reading SConscript files. 
scons: Building targets ... 
g++ -o ExampleClass.o -c ExampleClass.cc 
g++ -o ExampleClass.os -c -fPIC ExampleClass.cc 
ar rc libexample.a ExampleClass.o 
ranlib libexample.a 
g++ -o libexample.so -shared ExampleClass.os 
scons: done building targets. 

然后我可以安装,执行scons的与安装目标,如下:

# scons install 
scons: Reading SConscript files ... 
scons: done reading SConscript files. 
scons: Building targets ... 
Install file: "libexample.a" as "/home/notroot/projects/sandbox/libexample.a" 
Install file: "libexample.so" as "/home/notroot/projects/sandbox/libexample.so" 
scons: done building targets. 

或者,你可能只是做这一切与一个命令,先清理一切

# scons -c install 

然后,做这一切只用一个命令:

# scons install 
scons: Reading SConscript files ... 
scons: done reading SConscript files. 
scons: Building targets ... 
g++ -o ExampleClass.o -c ExampleClass.cc 
g++ -o ExampleClass.os -c -fPIC ExampleClass.cc 
ar rc libexample.a ExampleClass.o 
ranlib libexample.a 
g++ -o libexample.so -shared ExampleClass.os 
Install file: "libexample.a" as "/home/notroot/projects/sandbox/libexample.a" 
Install file: "libexample.so" as "/home/notroot/projects/sandbox/libexample.so" 
scons: done building targets.