5

I am trying to build both a static and a shared library using the same sources with SCons.

Everything works fine if I just build one or the other, but as soon as I try to build both, only the static library is built.

My SConscript looks like:

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)

I also tried SharedObject(sources) before the SharedLibrary (passing in the return from SharedObject, rather than sources), but it is no different. Same if I build the .a before the .so.

How do I work around this?

4

1 回答 1

6

当安装目录不在当前目录中或之下时,SCons 的行为与预期不同,如SCons 安装方法文档中所述:

但是请注意,安装文件仍被视为一种文件“构建”。当您记住 SCons 的默认行为是在当前目录中或当前目录下构建文件时,这一点很重要。如果如上例所示,您将文件安装在顶级 SConstruct 文件的目录树之外的目录中,则必须指定该目录(或更高的目录,例如 /)才能在其中安装任何内容:

也就是说,您必须以安装目录为目标(SP3_env['SP3_lib_dir']在您的情况下)调用 SCons 才能执行安装。为了简化这一点,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.
于 2013-03-03T11:21:58.153 回答