Gradle native pass to linker precompiled objects

(Mauro Manzo) #1

I’m porting a Makefile project in Gradle. There are several cpp files that need to be passed to an Oracle precompiler, which outputs .o objects files. This .o files are in turn to be passed to the linker along with the other “regular” .o files produced by g++.
How to reproduce this in Gradle?

Please note: I succeded in generating cpp files, and compiling-linking them with other regular cpp files, using a CppSourceSet. Unfortunately, I didn’t find an equivalent “CppObjectSet”. The only idea that came in my mind is to create a static library with all these .o files, and put it inside the project dependencies.

Any better idea?

Gradle Native - Execute task before binaries.all definition
(Walter von Westphalen ) #2

I had the same challenge with my build. I resolved it by defining a pre-defined library and adding a dependency on it on my component. I place it first so that it will added as the first library after the compiled object files (I found that gradle lists the libraries in the inputs in the order that they are defined).

repositories {
libs(PrebuiltLibraries) {

rwmys12 {
binaries.withType(StaticLibraryBinary) {
staticLibraryFile = file("$LIB_HOME/lib/rw/rwmys12s.o")


in the component

   mys_exe (NativeExecutableSpec) {
        sources {
            cpp {
                lib library: 'rwmys12'
                lib library: 'json'


this caused the linker options.txt to look like:


I tried to put it in the linker.args but that will place the .o file after the library list since gradle lists the linker arguments last.

(Daniel Lacasse) #3

Thanks Walter for providing your solution. It is the best one for the current state of the native support in Gradle regarding compilation only dependencies (aka object file dependencies).

I’m a bit late to the game but regarding an alternative, creating a static library with your object files should give your expected result in most cases.

I have opened an epic to capture the essential of this underlying work. Multiple issues will need to be addressed and will be part of that epic once we start working on improving this scenario.