4

我需要构建一个包含多个 C++ 项目的大型源代码树。我想要一个顶级构建文件,它定义构建约定,例如常见的包含目录和编译器/链接器标志,然后能够覆盖或添加到子项目级别的约定。

我能够添加到编译器/链接器标志如下:

顶级构建文件:

subprojects {
      apply plugin: 'cpp'
      ext.commonCompilerArgs = [
          '-Wall',
          '-Werror',
          '-pedantic-errors'
      ]

     components {
        binary(NativeLibrarySpec) {
          sources {
            cpp {
              source {
                srcDir 'src'
                include '*.cpp'
              }

              exportedHeaders {
                srcDir 'include'
                include '*.h'
              }
            }
          }
       }
     }
     binaries.withType(NativeLibraryBinarySpec) {
          // Compiler args
          commonCompilerArgs.forEach { compilerArg ->
          cppCompiler.args << compilerArg
          }
     }

子项目构建文件:

def extraCompilerArgs = [
  '--std=c++11'
]

binaries.all {
  extraCompilerArgs.each { arg ->
    cppCompiler.args << arg
  }
}

但是,我无法应用相同的原则来添加额外的包含目录(要添加到编译器参数中)。我在子项目构建文件中尝试了这个:

binaries.withType(NativeLibraryBinarySpec) { binary ->
  binary.headerDirs.add('extra_include_dir')
} 

但它不会添加额外的目录。我在这里做错了什么?

4

0 回答 0