【问题标题】:scons build both static and shared libraryscons 构建静态库和共享库
【发布时间】:2013-02-28 16:33:26
【问题描述】:

我正在尝试使用与 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 之前尝试了 SharedObject(sources)(传入来自 SharedObject 的返回,而不是源),但没有什么不同。如果我在 .so 之前构建 .a,则相同。

我该如何解决这个问题?

【问题讨论】:

    标签: scons


    【解决方案1】:

    当安装目录不在在当前目录中或之下时,SCons 的行为与预期不符,正如SCons Install method docs:中所注释的那样

    但是请注意,安装文件仍被视为一种 文件“构建”。当您记住默认值时,这一点很重要 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.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多