scons构建静态库和共享库

Bet*_*ich 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)
Run Code Online (Sandbox Code Playgroud)

我还在SharedLibrary之前尝试过SharedObject(sources)(传递来自SharedObject的返回,而不是源代码),但它没有什么不同.如果我在.so之前构建.a,则相同.

我该如何解决这个问题?

Bra*_*ady 6

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

但请注意,安装文件仍被视为一种文件"构建".当您记住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)
Run Code Online (Sandbox Code Playgroud)

如果我执行没有目标的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.
Run Code Online (Sandbox Code Playgroud)

然后我可以安装,使用安装目标执行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.
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用一个命令完成所有操作,首先清理所有内容

# scons -c install
Run Code Online (Sandbox Code Playgroud)

然后,只需一个命令即可完成所有操作:

# 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.
Run Code Online (Sandbox Code Playgroud)