Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gradle.pdf
Скачиваний:
9
Добавлен:
24.03.2015
Размер:
1.4 Mб
Скачать

47.1. Usage

The build scripts DSLs, model elements and tasks used to manage C++ projects are added by the plugin. However, it is typically more convenient to use either the cpp-lib or cpp-exe plugins that sit on top of the cpp plugin to preconfigure the project to build either a shared library or executable binary respectively.

Example 47.1. Using the 'cpp-exe' plugin

build.gradle

apply plugin: "cpp-exe"

Example 47.2. Using the 'cpp-lib' plugin

build.gradle

apply plugin: "cpp-lib"

The cpp-exe plugin configures the project to build a single executable (at $buildDir/binaries

) and the cpp-lib plugin configures the project to build a single shared library (at $buildDir/bi

).

47.2. Source code locations

Both plugins configure the project to look for .cpp and .c source files in src/main/cpp and use the src/main/headers directory as a header include root. For a library, the header files in src/m are considered the “public” or “exported” headers. Header files that should not be exported (but used internally) should be placed inside the src/main/cpp directory (though be aware that such header files should always be referenced in a manner relative to the file including them).

The cpp plugin is also very flexible in where it looks for source and header files, aand you can configure the above conventions to look however you like.

47.3. Compiling

For both the cpp-lib and cpp-exe plugins, you can run gradle compileMain to compile and

link the binary.

47.3.1. Compiling on UNIX

The UNIX C++ support is currently based on the g++ tool which must be installed and on the PATH for the Gradle process.

Page 260 of 343

47.3.2. Compiling on Windows

The Windows C++ support can use either the MinGW g++ or the Microsoft Visual C++ cl tool, either of which must be installed and on the PATH for the Gradle process. Gradle searches first for Microsoft Visual C++, and then MinGW.

47.4. Configuring the compiler

Arbitrary arguments can be provided to the compiler by using the following syntax:

Example 47.3. Supplying arbitrary args to the compiler

build.gradle

executables { main {

spec {

args "-fno-access-control", "-fconserve-space"

}

}

}

The above example applies to the cpp-exe plugin, to supply arguments for the cpp-lib plugin

replace “executables” with “libraries”.

47.5. Working with shared libraries

The C++ plugin provides an installMain task, which creates a development install of the executable, along with the shared libraries it requires. This allows you to run the executable without needing to install the shared libraries in their final locations.

47.6. Dependencies

Dependencies for C++ projects are binary libraries that export header files. The header files are used during compilation, with the compiled binary dependency being used during the linking.

47.6.1. External Dependencies

External dependencies (i.e. from a repository, not a subproject) must be specified using the following syntax:

Page 261 of 343

Example 47.4. Declaring dependencies

build.gradle

cpp {

sourceSets { main {

dependency group: "some-org", name: "some-lib", version: "1.0"

}

}

}

Each dependency must be specified with the dependency method as above and must be declared as part of the source set. The group, name and version arguments must be supplied.

For each declared dependency, two actual dependencies are created. One with the classifier “head

and extension “zip” which is a zip file of the exported headers, and another with the classifier “so

and extension “so” which is the compiled library binary to link against (which is supplied as a direct input to the g++ link operation).

47.6.2. Project Dependencies

The notation for project dependencies is slightly different.

Example 47.5. Declaring project dependencies

build.gradle

project(":lib") {

apply plugin: "cpp-lib"

}

project(":exe") {

apply plugin: "cpp-exe" cpp {

sourceSets { main {

libs << project(":lib").libraries.main

}

}

}

}

47.7. Publishing

The cpp-exe and cpp-lib plugins configure their respective output binaries to be publishable as

part of the archives configuration. To publish, simply configure the uploadArchives task as

per usual.

Page 262 of 343

Example 47.6. Uploading exe or lib

build.gradle

group = "some-org" archivesBaseName = "some-lib" version = 1.0

uploadArchives { repositories {

mavenDeployer {

repository(url: uri("${buildDir}/repo"))

}

}

}

The cpp-exe plugin publishes a single artifact with extension “exe”. The cpp-lib plugin publishes two artifacts; one with classifier “headers” and extension “zip”, and one with classifier “ ” and extension “so” (which is the format used when consuming dependencies).

Currently, there is no support for publishing the dependencies of artifacts in POM or Ivy files. Future versions will support this.

Page 263 of 343

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]