[Native] How can I add system libraries to link configuration?

(Benjamin Bjørnseth) #1

I am trying to link to a system-wide installed static library. Specifically, I have tried using the following in my build.gradle:

executables {
    test {
        binaries.all {
            linker.args "-lCppUTest"

Using gradle version 1.9, this adds the option before the project object files to link. This is problematic, as the library providing symbols must come after the object files using the symbols in the link command line. I have tried adding the system library as a dependency on the source set using the lib-method, but all libraries added this way seem to be interpreted as project-local libraries. Reading the guide http://www.gradle.org/docs/current/userguide/nativeBinaries.html points to the dependency-method as a way of marking dependencies external to the project, but I cannot see how to use this to get the wanted behaviour.

Is there any way of adding the flag “-lCppUTest” to the end of the build command, or is using system libraries not supported?

Modelling installed libraries

Yep, the way we handle ‘-l’ and ‘-L’ options isn’t great. Currently, the only way to achieve what you want is to re-order the command-line arguments just prior to their being passed to the linker:

model {

toolChains {

gcc(Gcc) {

linker.withArguments { args ->

if (args.remove(’-lCppUTest’)) {

args << ‘-lCppUTest’






Expect a better solution in the not-too-distant future.

(Benjamin Bjørnseth) #3

Great, thanks! It is good to hear that support for building native applications is actively developed; using gradle seems much more pleasurable than traditional tools, and I am looking forward to see how powerful future enhancements will make it.

Not that it is of great importance, a better solution being under development, but what you propose does not work on my system. It aborts with a ‘No signature of method withArguments applicable’-failure, see https://gist.github.com/anonymous/7659394. The gradle version I use is 1.9, downloaded a few days ago from the gradle downloads page; see https://gist.github.com/anonymous/7659437 for output from ‘gradle --version’. Is this perhaps an addition not yet released?

PS: Please consider my vote for also making any future enhancements available with the Clang toolchain :slight_smile:


I’d recommend using a recent nightly for all C++ related work: http://gradle.org/nightly. The released versions are lagging significantly in functionality.

The ‘Clang’ tool chain actually extends GccCompatibleToolChain and gets pretty much all of the Gcc-related enhancements.

(Franklin Mathieu) #5

I’m having the same issues – has there been a better way to do that since then ?


There’s ‘prebuilt libraries’ (see the sample) for libraries that are not necessarily ‘installed’ on the build machine, but you have the binaries/headers available.

There’s no specific support for ‘installed libraries’ yet.