# Does project dependencies supported for the CPP projects? Please share some example to define the project dependencies for CPP project

**URL:** <https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392>\
**Category:** Old Forum Archive\
**Created:** [November 4, 2014, 10:48am UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392 "2014-11-04T10:48:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mandar\_choure](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandar\_choure](https://discuss.gradle.org/u/mandar_choure)\
**Post date:** [November 4, 2014, 10:48am UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/1 "2014-11-04T10:48:00Z")

</div>

Hi, Please tell me the the way to define project dependencies supported for the CPP projects.

I do see this is supported for Java code, however I tried to use same syntax to define project dependencies for CPP project but its not working. like :

dependencies {

compile project(’:shared’), project(’:api’)

}

Thanks Mandar

---

<div class="post-metadata">

**Author:** ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)\
**Post date:** [November 4, 2014, 3:31pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/2 "2014-11-04T15:31:00Z")

</div>

Yes, they are supported, but they are defined differently. Typically, you would define them on the source set:

```gradle
sources {
    app.cpp.lib project: ':mylib', library: 'somelib'
    hello.cpp.lib project: ':util', library: 'util', linkage: 'static'
}

```

You can also set them at the binary level:

```gradle
binaries.all {
    lib project: ':mylib', library: 'somelib'
}

```

---

<div class="post-metadata">

**Author:** ![mandar\_choure](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandar\_choure](https://discuss.gradle.org/u/mandar_choure)\
**Post date:** [November 5, 2014, 1:00pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/3 "2014-11-05T13:00:00Z")

</div>

Thanks Gary for reply.

This is working fine when I have projects with some library dependency. However I am not able to solve the below scenario with this approach.

- We have multiple projects and those not necessarily library depends on each other, but we need to follow some specific sequence of execution of projects.

As some projects are completely independent but this will publish header files to common folder and that will be referred by the subsequent project. So this scenario I am not able solve using gradle.

Summary is, How to set project dependency without linking dependant library?

Thanks Mandar

---

<div class="post-metadata">

**Author:** ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)\
**Post date:** [November 5, 2014, 1:21pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/4 "2014-11-05T13:21:00Z")

</div>

Well, instead of using a common header directory, one thing you can do is create a dependency on the headers by depending on the sourceset:

```gradle
sources {
    app.cpp.lib project(':mylib').sources.somelib.cpp
}

```

Outside of that, assuming you have some some task that copies the headers to the common directory, you can just create a task dependency:

```gradle
executables {
    app {
        binaries.all {
            tasks.withType(CppCompile) {
                dependsOn project(':mylib').copyHeadersToCommon
            }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![mandar\_choure](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandar\_choure](https://discuss.gradle.org/u/mandar_choure)\
**Post date:** [November 6, 2014, 3:01pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/5 "2014-11-06T15:01:00Z")

</div>

Thanks Gary, this solved my problem.

Similarly one more thing, lets say I have project structure as: Root

|\_\_ A

|\_\_ build.gradle

|\_\_ B

|\_\_ build.gradle

|\_\_ settings.gradle

|\_\_ build.gradle

Here Project A and Project B are isolated projects. So how to execute both projects A & B from the Root directories build.gradle?

Thanks Mandar

---

<div class="post-metadata">

**Author:** ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)\
**Post date:** [November 6, 2014, 5:28pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/6 "2014-11-06T17:28:00Z")

</div>

I’m not sure I understand what you mean by “execute both projects” - presumably you want to execute some set of tasks from the subprojects using a single task in the root project?

If so, I would inject into each subproject a task that aggregates all tasks that I want to run. For instance, in the root build.gradle:

```gradle
subprojects {
    task compile {
        dependsOn tasks.withType(CppCompile)
    }
}

```

Then you can just run “gradle compile” from the root and it will run all “compile” tasks in all subprojects. Similarly, you can run the same from a subproject and it will run all compile tasks for that particular project.

You could also create an aggregator task in the root project that depends on individual subproject tasks if you need to be more explicit.

```gradle
task compileAndB {
    dependsOn project(':A').tasks.withType(CppCompile)
    dependsOn project(':B').tasks.withType(CppCompile)
}

```

---

<div class="post-metadata">

**Author:** ![mandar\_choure](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandar\_choure](https://discuss.gradle.org/u/mandar_choure)\
**Post date:** [November 10, 2014, 8:34am UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/7 "2014-11-10T08:34:00Z")

</div>

Thanks Gary, It does the compilation only. Actually I want to execute all the default task specified in those projects, not only compile. How do I do this.

Thanks Mandar

---

<div class="post-metadata">

**Author:** ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)\
**Post date:** [November 10, 2014, 12:50pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/8 "2014-11-10T12:50:00Z")

</div>

```gradle
dependsOn project(':A').defaultTasks

```

---

<div class="post-metadata">

**Author:** ![mandar\_choure](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandar\_choure](https://discuss.gradle.org/u/mandar_choure)\
**Post date:** [December 2, 2014, 7:36am UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/9 "2014-12-02T07:36:00Z")

</div>

Hi Gary, This is not working as expected, always getting the “UP-TO-DATE” for that task.

This is what I did: Project structure:

Root

|\_ A ( creates static lib and build.gradle having some default tasks)

|\_ B (creates dynamic lib and build.gradle having some default tasks)

Now in the Root project’s build.gradle having one task.

```gradle
task runDefaultTasks {
    dependsOn project(':A').defaultTasks
    dependsOn project(':B').defaultTasks
}
  -----------
settings.gradle file
include 'A'
include 'B'

```

Now when I run the “gradle runDefaultTasks”

output is : :runDefaultTasks UP-TO-DATE

Am I missing anything here?

---

<div class="post-metadata">

**Author:** ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)\
**Post date:** [December 2, 2014, 3:14pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/10 "2014-12-02T15:14:00Z")

</div>

Sorry, I forgot that defaultTasks is a list of Strings instead of Task objects. Evaluation order gets in the way a little bit, too. The following will work:

```gradle
def defaultTasksOf(Project project) {
    return project.defaultTasks.collect { project.tasks."$it" }
}
  task runDefault {
    dependsOn { defaultTasksOf project(':a') }
    dependsOn { defaultTasksOf project(':b') }
}

```

---

<div class="post-metadata">

**Author:** ![mandar\_choure](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mandar\_choure](https://discuss.gradle.org/u/mandar_choure)\
**Post date:** [December 2, 2014, 8:04pm UTC](https://discuss.gradle.org/t/does-project-dependencies-supported-for-the-cpp-projects-please-share-some-example-to-define-the-project-dependencies-for-cpp-project/7392/11 "2014-12-02T20:04:00Z")

</div>

Thanks Gary, It perfectly working as I expected.
