How to order object files for a linker task cpp-application (say linkDebug or linkRelease)


I’m porting some of our applications from make file system to Gradle based system. In our legacy make system, we control the order of object files to control static initialization order.


 gcc -o output Foo.o Bar.o 

// controlling the linking order, to guarantee Foo objects get initialized before Bar

Whereas in Gradle, the system controls the compiling and the linking order of object files.

I see LinkExecutable expose some methods (e…g, source) that will help me add new objects to the LinkerArgs.

Is it possible for me to control the linking order of object files, to help me get around static initialization issues?

I had some luck manipulating the compile order, but didn’t seem to make an impact on the linker order of objects, still running into the issue.

   binaries.configureEach {
           def compileTask = compileTask.get()
           compileTask.source.from fileTree(dir: "src/main/cpp", include: "**/foo.cpp")
           compileTask.source.from fileTree(dir: "src/main/cpp", include: "**/bar.cpp")   

I have the same need from a different use case where objects retrieved from a remote HTTP build cache cause link issues.

Did you find a way to handle this?