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

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

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

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:

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

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

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:

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:

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

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

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:

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.

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

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

dependsOn project(':A').defaultTasks

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.

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?

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:

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

Thanks Gary, It perfectly working as I expected.