Gradle C Plugin by Example
OK I figured all 3 out, and thought I would post this answer for any future readers.
Please note: This solution is really on viable for modern C programmers who:
- Want to do all development in Eclipse, taking advantage of modern IDE facilities like syntax highlighting, error, goto declaration, open call hierarchy, Eclipse's debugger, etc.; but...
- Want a modern, kick-a** build system like Gradle to do all the command-line/shell building
Furthermore, because I'm on Windows, I chose to use MinGW for my GCC provisioning, and so if you're either on *nix or Mac, or if you prefer Cygwin, you'll have to customize this solution even further.
Even furthermore, I have only verified this works with Eclipse Luna, using the latest Eclipse CDT plugin (8.6) and using Gradle 2.3.
Solution
First I had to correct my usage of the C plugin, changing my build.gradle
to look like this:
apply plugin: 'c'
apply plugin: 'eclipse'
model {
components {
derpus(NativeExecutableSpec) {
sources {
c(CSourceSet) {
source {
srcDir "src/derpus/c"
include "**/*.c"
}
exportedHeaders {
srcDir "src/derpus/headers"
}
}
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
This allowed me to run gradle wrapper
without any errors.
Next, I began to find it very peculiar that the Gradle Native Binaries documentation never mentions the build invocation for compiling/building native executables. I took a wild guess that it might be leveraging Gradle's "convention over configuration" approach, and I ran gradlew build
- voila! Great success. Now under derpus/build/binaries/derpusExecutable
I have derpus.exe
! So far, so good.
The real headache sets in when you want to now import this Gradle-managed project into Eclipse, but still have Eclipse CDT provide all the normal features of a modern C IDE.
I started off by running gradlew eclipse
, which added the following files under the derpus/
project root:
.project
.settings/language.settings
I opened Eclipse and imported it as a project, however I got all sorts of errors, and when I hovered over #include <stdio.h>
in my derpus.c
file, and clicked F3
, Eclipse did nothing. Clearly something was still not configured right. And so I got to hacking.
Turns out you just need to:
- Of course, first make sure the CDT plugin is installed and working correctly (doh!)
- Create a "dummy" C project in Eclipse, which will allow you to copy n' paste Eclipse CDT-generated settings/configs to your actual project
- Modify your actual
.project
file to include the same<buildSpec />
and<natures />
elements that were generated in the dummy project's.project
file - Copy the dummy project's
.cproject
file over to your actual project's root, and then open it in a text editor. You want to rename ALL instances of the dummy project's name with the name of your actual project; in my case there were 3 instances. In my case, my dummy project was literally nameddummy
, and my actual project is namedderpus
. So I had to change 3 instances ofdummy
toderpus
in this file. - Restart Eclipse
Your actual project will now behave exactly the same way as a C project created with the CDT plugin. Don't forget to delete you "dummy" project ;-)