代码之家  ›  专栏  ›  技术社区  ›  James Morris

为什么fltk config的输出会截断gcc的参数?

  •  1
  • James Morris  · 技术社区  · 14 年前

    我正在尝试构建一个我下载的应用程序,它使用SCONS“makereplacement”和fastlight工具包Gui。

    guienv = Environment(CPPFLAGS = '')
    guiconf = Configure(guienv)
    
    if not guiconf.CheckLibWithHeader('lo', 'lo/lo.h','c'):
        print 'Did not find liblo for OSC, exiting!'
        Exit(1)
    
    if not guiconf.CheckLibWithHeader('fltk', 'FL/Fl.H','c++'):
        print 'Did not find FLTK for the gui, exiting!'
        Exit(1)
    

    不幸的是,在我的(gentoolinux)系统和许多其他系统(Linux发行版)上,如果包管理器允许同时安装FLTK-1和FLTK-2,这可能会非常麻烦。

    我试图修改SConstruct文件以使用 fltk-config --cflags fltk-config --ldflags (或 fltk-config --libs ldflags

    guienv.Append(CPPPATH = os.popen('fltk-config --cflags').read())
    guienv.Append(LIBPATH = os.popen('fltk-config --ldflags').read())
    

    但这会导致liblo的测试失败!往里看 config.log 显示它是如何失败的:

    scons: Configure: Checking for C library lo...
    gcc -o .sconf_temp/conftest_4.o -c "-I/usr/include/fltk-1.1 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT"
    gcc: no input files
    scons: Configure: no
    

    os.popen( 'command').read()

    编辑 这里真正的问题是为什么附加 fltk-config

    2 回复  |  直到 14 年前
        1
  •  1
  •   Imran.Fanaswala    14 年前

    有两种类似的方法:

    conf = Configure(env)
    status, _ = conf.TryAction("fltk-config --cflags")
    if status:
      env.ParseConfig("fltk-config --cflags")
    else:
      print "Failed fltk"
    

    2)

      try:
        env.ParseConfig("fltk-config --cflags")
      except (OSError):
        print 'failed to run fltk-config you sure fltk is installed !?'
        sys.exit(1)
    
        2
  •  1
  •   Community CDub    7 年前

    这是一个相当复杂的问题,没有快速的答案

    我已经参考了使用说明 pkg-config http://www.scons.org/wiki/UsingPkgConfig . 下面的问题也很有帮助 Test if executable exists in Python? .

    经过大量调查我发现 os.popen('command').read()

    我们可以使用str.rstrip()删除尾部的'\n'。

    其次,作为 config.log fltk-config 提供,SCONS在给GCC之前用双引号括起来。我不确定具体细节,但这是因为 fltk配置 (通过 os.popen

    我们可以用 strarray = str.split(" ", str.count(" ")) 将输出拆分为出现空格字符的子字符串。

    同样值得注意的是,我们试图附加 fltk-config --ldflags 对于GUI环境中的错误变量,应该将它们添加到 LINKFLAGS

    我们需要做的是:

    • 将参数传递给可执行文件并捕获其输出
    • 将输出转换为适当的格式以附加到 CPPFLAGS 链接标志

    所以我定义了一些函数来帮助。。。

    1) 在系统上查找可执行文件的完整路径: )

    def ExecutablePath(program):
        def is_exe(fpath):
            return os.path.exists(fpath) and os.access(fpath, os.X_OK)
        fpath, fname = os.path.split(program)
        if fpath:
            if is_exe(program):
                return program
        else:
            for path in os.environ["PATH"].split(os.pathsep):
                exe_file = os.path.join(path, program)
                if is_exe(exe_file):
                    return exe_file
        return None
    

    def CheckForExecutable(context, program):
        context.Message( 'Checking for program %s...' %program )
        if ExecutablePath(program):
            context.Result('yes')
        return program
        context.Result('no')
    

    def ExecutableOutputAsArray(program, args):
        pth = ExecutablePath(program)
        pargs = shlex.split('%s %s' %(pth, args))
        progout = subprocess.Popen( pargs , stdout=subprocess.PIPE).communicate()[0]
        flags = progout.rstrip()
        return flags.split(' ', flags.count(" "))
    

    一些用法:

    guienv.Append(CPPFLAGS =  ExecutableOutputAsArray('fltk-config', '--cflags') )
    guienv.Append(LINKFLAGS = ExecutableOutputAsArray('fltk-config', '--ldflags') )
    guienv.Append(LINKFLAGS = ExecutableOutputAsArray('pkg-config', '--libs liblo') )