Gradle 3.0 - PrebuiltLibraries dependency link error


(Brandon Lien) #1

I have a C++ project and trying to link against a library built in another project that compiles a C library with gcc.

If I build the first project “A” and use the following in “B” which uses cpp plugin I can compile and run fine. However, I fail from a clean build with parallelization, because the prebuilt library isn’t there yet.

What I would like is a mechanism that I can force A’s build to run to completion before reaching B’s linkTestExecutable that tries to link my “prebuilt” library. My issue, is the use of some C includes won’t let me compile A with the g++ compiler.

What are my options?

apply plugin: 'cpp'

build.dependsOn([':A:build'])
clean.dependsOn([':A:clean'])

model {
    components {
        B (NativeExecutableSpec) {
         sources {
                cpp {
                    lib library: "myA", linkage: 'static'
                } 
            }
        }
    }
}

model {
    repositories {
        libs(PrebuiltLibraries) {
            myA { 
                def baseDir = "../A/build/libs/A/static/"
                headers.srcDir "../A/src/A/headers/"

                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = file("${baseDir}/libA.a")
                }
            }
        }
    }

    binaries {
        all {
            cppCompiler.args "-std=c++11", "-pthread","-fPIC","-ggdb", "-fext-numeric-literals"
        }
    }

#2

If project A and project B are both managed by the same Gradle build, then you should define “A” as a project dependency of “B” instead of defining “myA” as a PrebuiltLibrary. This would involve changing the cpp {} to:

model {
    components {
        B (NativeExecutableSpec) {
         sources {
                cpp {
                    // Make project B depend on project A's "A" library.
                    // This assumes the component (library) you've defined in project A is named "A"
                    lib project: ':A', library: 'A', linkage: 'static'
                } 
            }
        }
    }
}

If project A and project B are managed by different Gradle builds, then you may want to look into Gradle’s composite builds.


(Brandon Lien) #3

I think I’ve misrepresented my problem after reverting to something similar to your post @Kevin2. I can build using the standard ./gradlew build.

However I was trying to find a workaround to resolving the error i get when running: ./gradlew build --parallel.

I have a C++ library, statically linked against a C library, statically linked against a C executable. If I put my “lib project:” lines in a particular order it will build fine using build command.

i was trying to use the prebuilt as a mechanism to resolve the --parallel build option that causes the G++ link error.