C++ with gradle

2019-03-08 14:18发布

问题:

In Gradle 1.10 Release notes http://www.gradle.org/docs/current/release-notes I see C++ build mentioned.

How to set up C++ project to build with gradle? (without IDE)

Suppose I have

ProjectFolder/hello.cpp
ProjectFolder/build.gradle

hello.cpp:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    puts("Hello World!!!");
    return EXIT_SUCCESS;
}

What should basic build.gradle be for this C++ project?

UPDATE: I have already looked at Chapter 72 of User Guide and 2 year old examples mentioned. They don't make it simpler but more complicated.

There is 1 file example with 6 lines. I haven't been touching C++ for 10 year and I just wanted quick start e.g. with GCC . (Not yet found)

回答1:

  1. put this in build.gradle

    apply plugin: 'cpp'
    executables {
       hello {}
    }
    
  2. put your source file in src/hello/cpp/say_hello.cpp

  3. run 'gradle helloExecutable'

  4. your executable should be built to build/binaries/helloExecutable/hello

Or, if you want your source in src/foo/bar then add

sources {
    hello {
      cpp {
        source {
            srcDir "src/foo/bar"
        }
    }
}


回答2:

Starting on Gradle 2.3 there have been major changes to native-component builds and the executables and libraries containers are not available anymore. Citing Gradle 2.3 Release notes:

... the DSL for defining native executables and libraries has fundamentally changed. The executables and libraries containers have been removed, and components are now added by type to the components container owned by the model registry. Another major change is that source sets for a component are now declared directly within the component definition, instead of being configured on the sources block.

The updated Gradle code compatible with Gradle 2.3+ will therefore look like:

model {
  components {
    hello(NativeExecutableSpec) {
      sources {
        cpp {
          source {
            srcDir "src/foo/bar"
          }
        }
      }
    }
  }
}

You can learn more about the new model in Gradle user guide here.



回答3:

This is another answer. I am using Gradle 2.4.7. My source code is the standard C++ as in the following directory structure on Windows:

myproject

myproject\src

myproject\src\main.cpp

myproject\build.gradle

main.cpp is just a Hello world C++ program.

I have Visual Studio 2015. I like to use its C++ compiler for the standard C++ programs.

build.gradle is as follows:

apply plugin: 'cpp'

model {
    components {
        main(NativeExecutableSpec) {
            sources {   
                cpp {
                    source {
                        srcDir "src"
                    }
                }
            }   
        }
    }

    binaries {
        all {
            if (toolChain in VisualCpp) {
                cppCompiler.args "-IC:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.10240.0\\ucrt"
                linker.args "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.10240.0\\ucrt\\x86"
            }
        }
    }
}

The extra compiler option and linker option is due to some change in Visual studio 2015 from its previous versions. Without the options, you will get a compilation not able to finding corecrt.h or a linker error not able to finding libucrt.lib.

Hope this will help you to begin C++ compilation with Visual Studio 2015 quickly!!!



标签: c++ gradle