[CPP] Generated sources

Hey there,

I was busy with a Gradle script that generates .cpp files, and I need to compile those as well as my existing sources.
Sadly, the generation does not work.
It seems impossible to generate cpp files, and compile them together with my source cpp files.

To show you the exact problem I have, I created a minimal test repository here, which shows the problem when you try to build.
Additionally, I uploaded the Gradle scan output here.

I checked the following resources on this subject:

  1. How to use gradle with generated sources? - #3 by bioinfornatics
  2. Gradle does not build required C++ libraries if the sources are generated
  3. https://stackoverflow.com/questions/38379142/
  4. native-samples/cpp/source-generation at master · gradle/native-samples · GitHub

Sadly, sources 1-3 are severely outdated, and use the old cpp plugin instead of the modern cpp-application/cpp-library plugins (like I do). They are not useful.

Source 4 is good, but does not show how to manage situations with both generated and non-generated sources (like my situation).

Could somebody help me with this problem?

P.S: Maybe it is a good idea to add the minimal example from my repo to the official native-samples repo once it compiles?

P.P.S: I am not very experienced with Gradle yet.

To answer my own question:

We can generate cpp files and include them in the compilation process with the following gradle snippet:

tasks.withType(CppCompile).configureEach {
    dependsOn codeGeneration
}

This makes every cpp compilation task first evaluate codeGeneration, which generates the sources.
The generated sources will be included in the build process, if and only if the generated files are placed somewhere in the project’s srcDir and have the right extension.

E.g: Generating sources to src/main/cpp/generated/ all with .cpp/.h extension works well.
Generating sources to src/main/customDir/ does not work out of the box, because Gradle only looks for sources in src/main/cpp/ with the .cpp extension.

I hope this is all clear and helps fellow cpp-application/cpp-library users.

I updated the minimal test repository, which works well now.

(P.S: I’ll mark this answer as ‘the solution’. If other/better solutions are posted, I will either update my answer or change the solution.)