Saturday, October 01, 2016

Using Gradle to build the standard C++ using VC++ compiler in Visual Studio 2015

This is a post about using Gradle to build the standard C++ using VC++ compiler in Visual Studio 2015 quickly.

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!!!

No comments: