In Multiproject CPP build structure can we provide common compiler and linker options in root level project build.gradle?

Hi,

Pardon if this is very stupid question but I am newbie to gradle and somehow not able to get this working. We are trying to build a multi project build stucture using gradle for our C++ code base. typical structure would be Root

| – Lib1 | --Lib2 | --Exe 1 and so on…

I am trying to put all common compiler and linker options in root level build.gradle with binaries.all, but subproject build.gradles are not able to get these options while building sub projects.

Is there any configuration I need to do in subprojects as well?

Do you have each subproject included in settings.gradle?

I put this in my root build.gradle:

subprojects {
   apply plugin: 'cpp'
   binaries.all {
       cppCompiler.define 'TEST'
   }
}

This in my settings.gradle:

include 'lib1', 'lib2', 'exe1'

And then in lib1/build.gradle (similarly for lib2):

libraries { lib1 }

And in exe1/build.gradle:

executables { exe1 }

This added a define for TEST in all of my compiler options for all the libraries and executables.

Are all of your subprojects single component builds (one executable, one library, etc)? It might be simpler to define it as a single project with multiple components.

Thanks Sterling…

I was using subproject.all and it wasn’t working. This worked.

I have one more question. I was under impression after build gradle automatically adds generated lib’s or bin’s path in linker options and path variable. Is that not the case? My code is failing to link because generated libs are in build/ path and its not able to find it…

Do I need to explicitly add each generated build path in linker.args?

One thing to note is my codebase does not have standard code structure. so we are using CppCourceSet to provide all paths to compile files. can this be the reson?

also it will be great if you could provide pointers on how to change default path for all libs and bins so that they will be generated at specified folders.

You have to add dependencies (see http://gradle.org/docs/current/userguide/userguide_single.html#N15EBA) sort of like a Java project. You can change the location of the linked executables/libraries, but I’d only do that if you have a really good reason. The API is still incubating and subject to change. I think you’d have to change the defaults of the link or install tasks here: http://gradle.org/docs/current/dsl/index.html#N10729

Yep it worked… I was adding dependencies in same way for binaries and libraries. that’s why it did not work for binaries.

Can you please also tell how to add dependencies on tasks in multi project approach?

I had added task dependencies in following way when all tasks were automatically generated and I did not have sub projects defined.

task build project.tasks.build.dependsOn {[releaseLib1SharedLibrary, releaseLib2SharedLibrary]}

But same is not working when these are tasks auto generated for sub projects.

You have to qualify the path to the tasks.

e.g., if releaseLib1SharedLibrary is in subproject1.

project.tasks.build.dependsOn ":subproject1:releaseLib1SharedLibrary"

Thanks Sterling.

Apologies for so many questions.

We have C++ and C code clubbed in same project within multiple modules. Can you please let me know is this supported with gradle?

for C projects I am trying to use plugin ‘c’ but still its invoking g++ compiler and failing to compile.

is there any way I can specify which toolchain to use for different subprojects?

figured it out… Its working now :slight_smile:

Hi Sterling,

I have encountered a situation in our code where I need your help please.

We have a module which generates some cpp stub files with help of java and then those generated cpp files are compiled to generate library.

What should be approach for this situation? 1. Shall I have one project which will have one project with two tasks to generate cpp files first and then compile cpp files to genearte libarary 2. Or there should be two projects one for CPP and one for JAVA where CPP one will have dependency on Java

Appreciate your help…

Hi Minal.

Take a look at some of the samples in the Gradle distribution. In particular, look at the idl sample: https://github.com/gradle/gradle/blob/REL_2.2.1/subprojects/docs/src/samples/native-binaries/idl/build.gradle

If you have a library that has files that need to be generated, you can configure the SourceSet as generatedBy a task.

I can’t think of a reason to prefer one way over the other (multiple projects or one project) in this particular case.

If you have more questions, please open another thread so other people can jump in.

Thanks Sterling for your help. I could achieve this behavior with generatedBy SourceSet

Hi Sterling,

sorry for posting to same thread again but I am facing issues while adding include paths in windows CPP build.

The way I am trying is like below

if (toolChain in VisualCpp) {

cppCompiler.args “/nologo”, “/W3”, “/WX-”, “/Oy-”, “/Gm”, “/EHsc”, “/GS”, “/fp:precise”, “/Zc:wchar_t”, “/Zc:forScope”, “/GR”, “/Gd”, “/analyze-”, “/errorReport:queue”

cppCompiler.args “/D_WINDOWS”, “/D_AFX_NO_DB_SUPPORT”, “/D_CONSOLE”, “/DWIN32”, “/D_VC80_UPGRADE=0x0600”, “/D_MBCS”

cppCompiler.args “/D_AFXDLL”

tasks.withType(CppCompile) {

includes.from("$project.projectDir")

includes.from("$project.buildDir…")

includes.from(“I:\sybase\OCS-12_0\include”)

includes.from(“I:\OOToolKit\4.4.0\Windows_VC10\include\include”)

}

but I get error like below

What went wrong: Could not compile build file ‘H:\Work\CopiedCode\CopiedSourceCode\build.gradle’.

startup failed: build file ‘H:\Work\CopiedCode\CopiedSourceCode\build.gradle’: 207: unexpected char: ‘’ @ line 207, column 37. includes.from("$project.buildDir…") ^ 1 error

Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

I have already started another thread but I did not get any response on that.

http://forums.gradle.org/gradle/topics/how-can-we-build-windows-cpp-code-which-is-using-precompiled-pch-files

http://forums.gradle.org/gradle/topics/how-to-add-include-and-linker-paths-in-visual-studio-build

Can you please provide pointers for windows build using VisualCpp toolchain?