This works fine on my Fedora system, but when building on Ubuntu this fails because linkerOpts.linux is wrong. I need to change the link path to: /usr/lib/x86_64-linux-gnu.
This leads me to two questions:
Why do I have to specify the link path in the first place? Normally this is determined automatically, and different distributions use different paths. In fact, it’s impossible to predict which ones are needed.
Assuming there are some technical reasons why the link path can’t be set up automatically, how can I add some code that runs during build that computes this path automatically? Basically, I’d need to call out to some code that looks at ld.so.conf to determine which path should be used.
Thank you. I decided to add code to dynamically compute the linker options. For the regular build, this was rather easy as all I had to do was to add a linkerOpts parameter to the linuxX64.binaries.executable group.
When I do this, building the main binary works, but the tests fail because the test binary is not built with these flags. According to the documentation, I have to use linuxX64.binaries.test instead, like this:
But when I do this, even if the test group is completely empty, I get the following error from Gradle:
Build file '/home/elias/prog/array_kotlin/array/build.gradle' line: 46
A problem occurred evaluating project ':array'.
> Cannot create binary debugTest: binary with such a name already exists
How can I add linker options programmatically to both the normal and test builds?
Hello, @Loke! I think the first approach you tried (the one with a linuxX64.binaries.test) fails because of some misunderstanding. You’re specifying a new test task here, instead of changing the old one. I suppose that using binaries.getTest(DEBUG) would make more sense here. Or, maybe even binaries.all, to avoid duplicating lines. See the details here.
The second approach seems fine, I would only recommend you to generalize it a bit - one can share this configuration between test and main compilation using compilation.forEach {...} method.