Linking a shared library to an executable with cpp-application plugin

I’m using the cpp-application plugin of gradle 7.3 to compile a native executable on Ubuntu Linux.

I was able to get the executable to compile and link. However, when I try to run it, it gives the error:

<executable-path>: error while loading shared libraries: lib<my-library>.so: cannot open shared object file: No such file or directory

Normally, one solution to this problem is to use the LD_LIBRARY_PATH environment variable and set to to include the directory that contains lib<my-library>.so.

However, lib.so is actually in an artifact in a maven repo that was built using the gradle cpp-library plugin and published to the maven repo using the maven-publish plugin.

So, using LD_LIBRARY_PATH isn’t a good solution.

In the cpp-application plugin documentation https://docs.gradle.org/current/userguide/cpp_application_plugin.html, several dependency configurations are described, in particular implementation, but also mainDebugImplementation and nativeRuntimeDebug

When I use the implementation configuration:
application { dependencies { ... implementation(group="<mygroup>", name="<my-library>_debug", version="<version>") ... } }

I get the following error:

`FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:<project>:compileDebugCpp’.

Could not resolve all files for configuration ‘:<project>:cppCompileDebug’.
Could not resolve :_debug:.
Required by:
project :<project>
No matching variant of :_debug::20211209.055658-1 was found. The consumer was configured to find attribute ‘org.gradle.usage’ with value ‘cplusplus-api’, attribute ‘org.gradle.native.debuggable’ with value ‘true’, attribute ‘org.gradle.native.optimized’ with value ‘false’, attribute ‘org.gradle.native.operatingSystem’ with value ‘linux’, attribute ‘org.gradle.native.architecture’ with value ‘x86-64’ but:
- Variant ‘debugLink’ capability :_debug: declares attribute ‘org.gradle.native.architecture’ with value ‘x86-64’, attribute ‘org.gradle.native.debuggable’ with value ‘true’, attribute ‘org.gradle.native.operatingSystem’ with value ‘linux’, attribute ‘org.gradle.native.optimized’ with value ‘false’:
- Incompatible because this component declares attribute ‘org.gradle.usage’ with value ‘native-link’ and the consumer needed attribute ‘org.gradle.usage’ with value ‘cplusplus-api’
- Variant ‘debugRuntime’ capability :_debug: declares attribute ‘org.gradle.native.architecture’ with value ‘x86-64’, attribute ‘org.gradle.native.debuggable’ with value ‘true’, attribute ‘org.gradle.native.operatingSystem’ with value ‘linux’, attribute ‘org.gradle.native.optimized’ with value ‘false’:
- Incompatible because this component declares attribute ‘org.gradle.usage’ with value ‘native-runtime’ and the consumer needed attribute ‘org.gradle.usage’ with value ‘cplusplus-api’`

However, the other configurations, e.g. mainDebugImplementation and nativeRuntimeDebug appear not to be available. Using them results in the error:

`Script compilation error:

Line 91: nativeRuntimeDebug(group=“”, name=“_debug”, version=“”)
^ Unresolved reference: nativeRuntimeDebug`

So, my question is: How can you get the cpp-application plugin to link against an artifact in a maven repo that was built by the cpp-library plugin and uploaded by the maven-publish plugin?