C/C++ How to build an executable?


(Joseph Fota) #1

I can build the libraries.

How tell gradle that I want to build the example executables? Specifically, libhbase_test.c

apply plugin: 'c'
apply plugin: 'cpp'
import org.apache.tools.ant.taskdefs.condition.Os
    ext {
     javaHome = System.getenv('JAVA_HOME')
    javaHomeInclude = javaHome + '/include'
      if (System.properties['os.name'].toLowerCase().contains('linux'))
    {
        javaHomeIncludeOs = javaHomeInclude + '/linux'
    }
    else
    {
        javaHomeIncludeOs = javaHomeInclude + '/win32'
    }
}
    task wrapper(type: Wrapper) {
    description 'A wrapper which downloads and/or uses Gradle 1.10-rc-1'
    gradleVersion = '1.10-rc-1'
}
    dependencies {
    if (! javaHome) {
        logger.error('JAVA_HOME is not set.')
    }
          println 'JAVA_HOME: ' + javaHome
}
    /*
 * Type of library being built.
 */
libraries {
    'libhbase-jni' {
        binaries.withType(SharedLibraryBinary)
    }
}
    /*
 * A list of where all the c and c++ sources can
 * be found and the headers files needed for each
 * component.
  */
 sources {
    'libhbase-jni' {
        cpp {
            source {
                srcDirs "src/jni_impl"
                include "*.cc"
            }
            exportedHeaders {
                srcDirs "include", javaHomeInclude, javaHomeIncludeOs
                include "**/*.h"
            }
        }
        c {
            source {
                srcDirs "src/common"
                include "*.c"
            }
            exportedHeaders {
                srcDirs "include"
                include "*.h"
            }
        }
    }
}
    /*
 * To build the libhbase-jni shared library:
 *
 *
 $ ./gradew build-libhbase-jni
 *
 */
task 'build-libhbase-jni' (dependsOn: 'libhbase-jniSharedLibrary') {
    description 'Build libhbase-jni shared library.'
}
  executable {
  // something magical goes here
}

#2

Defining an executable is much the same as defining a library. You give the executable a name, and there is a matching sourceSet with the same name.

Take a look at the various samples in ‘samples/native-binaries’.


(Joseph Fota) #3

Unfortunately, my situation doesn’t follow the src/main/{cpp,headers} example.

I have /example/libhbase_test.c, /include. I guess I’m looking for examples that show me how to make gradle fit what have and not the other way around.


(Joseph Fota) #4

Unfortunately, my situation doesn’t follow the src/main/{cpp,headers} example. I have /example/libhbase_test.c, /include. I guess I’m looking for examples that show me how to make gradle fit what have and not the other way around.


#5
executables {

 libhbase_test {} } sources {

 libhbase_test {



 c {





 source {







  srcDir "/example"







  include "*.c"





 }





 exportedHeaders {







  srcDir "/include"





 }

 

Unfortunately we don't yet have a way to define private headers. That's planned.


(Joseph Fota) #6

Thank you.


(Joseph Fota) #7

I’m posting this solution for others that may be looking for C/C++ examples.

apply plugin: 'c'
apply plugin: 'cpp'
import org.apache.tools.ant.taskdefs.condition.Os
model {
    buildTypes {
        release
    }
    repositories {
        libs(PrebuiltLibraries) {
            jvm {
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("/usr/lib/jvm/java-7-openjdk-amd64/jre/lib/amd64/server/libjvm.so")
                }
            }
        }
    }
}
ext {
     javaHome = System.getenv('JAVA_HOME')
    javaHomeInclude = javaHome + '/include'
    if (System.properties['os.name'].toLowerCase().contains('linux'))
    {
        javaHomeIncludeOs = javaHomeInclude + '/linux'
    }
    else
    {
        javaHomeIncludeOs = javaHomeInclude + '/win32'
    }
}
dependencies {
    if (! javaHome) {
        logger.error('JAVA_HOME is not set.')
    }
    println 'JAVA_HOME: ' + javaHome
}
/*
 * libhbase-jni shared library
 */
libraries {
    'libhbase-jni' {
        binaries.withType(SharedLibraryBinary)
     }
}
sources {
    'libhbase-jni' {
        cpp {
            source {
                srcDirs "src/jni_impl"
                include "*.cc"
            }
            exportedHeaders {
                srcDirs "include", javaHomeInclude, javaHomeIncludeOs
                include "**/*.h"
            }
        }
        c {
            source {
                srcDirs "src/common"
                include "*.c"
            }
            exportedHeaders {
                srcDirs "include"
                include "*.h"
            }
        }
    }
}
/*
 * libhbase_test executable
 */
executables {
    libhbase_test {
        binaries.all {
            lib libraries['libhbase-jni'].shared
             cCompiler.args "-ljvm", "-std=gnu99"
         }
    }
}
sources {
    libhbase_test {
        c {
            source {
                srcDir "example"
                include "*.c"
            }
            exportedHeaders {
                srcDir "include"
            }
             lib library: 'jvm'
        }
    }
}
/*
 * Build tasks
 */
task wrapper(type: Wrapper) {
    description 'A wrapper which downloads and/or uses Gradle 1.11-20140109230012+0000'
    gradleVersion = '1.11-20140109230012+0000'
}
task 'libhbase-jni' (dependsOn: 'libhbase-jniSharedLibrary') {
    description 'Build libhbase-jni shared library.'
}
task 'libhbase_test' (dependsOn: 'libhbase_testExecutable') {
    description 'Build libhabase_test executable.'
}