Gradle C plugin as an example

In my local file system, I have the following C project directory structure:

derpus/ src/ derpus/ c/ derpus.c headers/ build.gradle 

Where derpus.c :

 #include <stdio.h> #include <stdlib.h> int main(void) { puts("Derp!"); return EXIT_SUCCESS; } 

I would like to use the Gradle Native (C) Plugin to manage the full range of derpus builds. In particular, I would like Gradle:

  • Create a Gradle wrapper so that I can use gradlew for all my build requests; and
  • Compile and build derpus in derpus.exe via gradlew ; and
  • Create Eclipse project information when I run gradlew eclipse to then import the project into Eclipse (I already preinstalled the Eclipse CDT plugin)

Here is my build.gradle :

 apply plugin: 'c' apply plugin: 'eclipse' sources { c { source { srcDir "src/derpus/c" include "**/*.c" } exportedHeaders { srcDir "src/derpus/headers" } } } task wrapper(type: Wrapper) { gradleVersion = '2.3' } 

Obviously, I have to run the gradle wrapper to take care of the first element. But as far as compiling and building, no, where in the documents of the C plugin I really see a command or build a call that actually launches compilation and assembly!

As for the third element, using the Eclipse plugin and calling it via gradlew eclipse , I wonder if there is anything else I need to make the resulting project / settings configurations compatible with what the Eclipse CDT plugin expects to work with C. programs. Although I intend to let Gradle handle all my builds, I still want to do all my development in Eclipse, and therefore everything that CDT comes across is important to me (syntax highlighting, compilation, etc.).

+6
source share
3 answers

OK. I understood all three, and thought that I would publish this answer for future readers.

Please note: this solution is really viable for modern C programmers who:

  • Want to do all the development in Eclipse using modern IDE tools such as syntax highlighting, error, goto declaration, open call hierarchy, Eclipse debugger, etc .; but ...
  • Want a modern push-a ** build system like Gradle to do all the command line / shell building

In addition, since I am on Windows, I decided to use MinGW for my GCC software, so if you are either on * nix or Mac, or if you prefer Cygwin, you will have to configure this solution even later.

In addition, I only confirmed that this works with Eclipse Luna, using the latest Eclipse CDT plugin (8.6) and using Gradle 2.3.

Decision

First I had to fix my use of the C plugin by 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 started very strange that the Gradle Native Binaries documentation never mentions a build call to compile / create your own executables. I figured it could be using the Gradle "configuration agreement" approach, and I ran gradlew build - voila! Big success. Now under derpus/build/binaries/derpusExecutable I have derpus.exe ! So far so good.

The real headache comes when you want to import this Gradle project into Eclipse, but still the Eclipse CDT has all the usual features of a modern C IDE.

I started by running gradlew eclipse , which added the following files to the root of the derpus/ project:

  • .project
  • .settings/language.settings

I opened Eclipse and imported it as a project, however I had all kinds of errors, and when I hung over #include <stdio.h> in the derpus.c file and pressed F3 , Eclipse did nothing. Obviously, something is still not configured correctly. And so I had to hack.

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 that allows you to copy n 'paste the Eclipse CDT-created settings / configs into your actual project
  • Modify the actual .project file to include the same <buildSpec /> and <natures /> elements that were generated in the .project file's .project file
  • Copy the dummy project .cproject to your actual project root, and then open it in a text editor. You want to rename ALL instances of the name of the dummy project with the name of your actual project; in my case there were 3 cases. In my case, my dummy project was literally called dummy , and my actual project is called derpus . So I had to change 3 dummy instances to derpus in this file.
  • Restart Eclipse

Your actual project will now behave exactly like a C project created using the CDT plugin. Remember to delete the "dummy" project; -)

+6
source

You can add the nature of C ++ to the project with:

  • create gradle project
  • select project
  • use eclipse-> new-> other
  • select C ++ -> convert to C ++ project
  • clear project
+1
source

Just tried this (with Gradle 4.3) and I would like to point out that you can simplify the model section so that:

 model { components { derpus(NativeExecutableSpec) } } 

because the source directory is "src/derpus/c" by default.

0
source

Source: https://habr.com/ru/post/986731/


All Articles