SCons的库和分库 [英] SCons libraries and sub-libraries

查看:195
本文介绍了SCons的库和分库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于SCons的分层构建系统。我有一个根SConstruct调用成构建一个共享库,然后到一个不同的SConscript的建立依赖于共享库可执行文件的SConscript。

I have a hierarchical build system based on SCons. I have a root SConstruct that calls into a SConscript that builds a shared library and then into a different SConscript that builds an executable that depends on the shared library.

因此​​,这里是我的问题:我对Linux共享库的理解是,当你想要做最后的 LD 链接的可执行文件将使用共享库,该共享库已被包含在可执行文件的 LD 作为源引用它(除非它是在一个标准的位置命令行中这种情况下, - →选项工程)。

So here's my question: my understanding of shared libraries on linux is that when you want to do the final ld link for the executable that will be using the shared lib, the shared lib has to be included on the executable's ld command line as a source to reference it (unless it's in a standard location in which case the -l option works).

所以这里的东西就像我SCons的文件看起来是这样的:

So here's something like what my SCons files look like:

=== ROOTDIR / SConstruct

=== rootdir/SConstruct

env=DefaultEnvironment()
shared_lib = SConscript('foolib/SConscript')
env.Append( LIBS=[shared_lib] )
executable = SConscript('barexec/SConscript')

=== ROOTDIR / foolib / SConscript

=== rootdir/foolib/SConscript

env=DefaultEnvironment()
env.Append(CPPPATH=Glob('inc'))
penv = env.Clone()
penv.Append(CPPPATH=Glob('internal/inc'))
lib = penv.SharedLibrary( 'foo', source=['foo.c', 'morefoo.c']
Return("lib")

=== ROOTDIR / barexec / SConscript

=== rootdir/barexec/SConscript

env=DefaultEnvironment()
exe = env.Program( 'bar', source=['main.c', 'bar.c', 'rod.c'] )
Return("exe")

所以这里的结是这一行:

So the hitch here is this line:

env.Append( LIBS=[shared_lib] )

这将产生的库添加到那些需要它们的任何其他库的命令行的好方法,但由于SCons的是做了两通经SConscripts运行(第一个生成它的依赖关系树,然后做工作), ROOTDIR / foolib / libfoo.so 所有产品的命令行,即使 libfoo.so 本身:

This would be a great way to add generated libraries to the command line for any other libs that need them, EXCEPT that because SCons is doing a two-pass run through the SConscripts (first to generate it's dependency tree, then to do the work), rootdir/foolib/libfoo.so winds up on the command line for ALL products, EVEN libfoo.so itself:

gcc -g -Wall -Werror -o libfoo.so foo.o morefoo.o libfoo.so

所以,这是怎么用最好SCons的呢?现在我已经使出了这个技巧:

So how is this best done with SCons? For now I've resorted to this hack:

=== ROOTDIR / SConstruct

=== rootdir/SConstruct

env=DefaultEnvironment()
shared_lib = SConscript('foolib/SConscript')
env['shared_lib'] = shared_lib
executable = SConscript('barexec/SConscript')

...

=== ROOTDIR / barexec / SConscript

=== rootdir/barexec/SConscript

env=DefaultEnvironment()
exe = env.Program( 'bar', source=['main.c', 'bar.c', 'rod.c'] + env['shared_lib'] )
Return("exe")

是否有这样做的更SCons的-Y方式?

Is there a more SCons-y way of doing this?

推荐答案

您应该允许通过构建中发现的共享库。

You should allow the shared libraries to be found by the build.

查找在 SCons的 LIBPATH RPATH 变量C>文件;这些都是使用SCons-Y的方式来设置搜索路径,这样产生的任何 -l <​​/ code>选项找到正确的库。

Look for the LIBPATH and RPATH variables in the SCons documentation; these are the "Scons-y" way to set up search paths so that any generated -l options find libraries properly.

说完上面所说的,这里就是你的的看到 GCC 基于SCons的(的设置做,如果没有,你可能需要手动做)。

Having mentioned the above, here's what you should see gcc do based on the setup of SCons (and if it doesn't, you may have to do it manually).

-l <​​/ code>选项总是能找到共享库提供的,你也给编译器库的位置。有此需要两次:在编译时( -L 选项),并在运行时( -rpath 生成的链接器选项​​)。

The -l option always finds shared libraries provided that you also give the compiler the location of the library. There are two times this is needed: at compile time (-L option) and at runtime (-rpath generated linker option).

LIBPATH SCons的设置应该产生一些看起来像 -L /一些/目录/路径的编译 - 时间搜索路径。

The LIBPATH SCons setup should generate something that looks like -L/some/directory/path for the compile-time search path.

RPATH SCons的设置应该产生一个链接器选项的嵌入的搜索路径;例如 -Wl,-rpath -Wl,\\ $ ORIGIN /../ lib目录将嵌入搜索相对于可执行文件的搜索路径,使可执行文件放置在在并行斌搜索 LIB 安装的目录。

The RPATH SCons setup should generate a linker option to embed a search path; e.g. -Wl,-rpath -Wl,\$ORIGIN/../lib would embed a search path that searches relative to the executable so that executables placed in bin search in the parallel lib directory of the installation.

这篇关于SCons的库和分库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆