ORNEW

Setup Gradle for C++

Facebook にシェア
Pocket

Installation

For Mac OS X. If Linux, it’s the same way. Windows? I don’t know.

First of all, install sdkman. (fka:gvm)

http://sdkman.io/install.html

$ export SDKMAN_DIR="/usr/local/sdkman" && curl -s "https://get.sdkman.io" | bash

In Bash, initialize sdkman by source "/usr/local/sdkman/bin/sdkman-init.sh". I recommend that you add the following commands to ~/.bash_profile:

$ cat << EOS >> ~/.bash_profile
> 
> export SDKMAN_DIR="/usr/local/sdkman"
> [[ -s "/usr/local/sdkman/bin/sdkman-init.sh" ]] && source "/usr/local/sdkman/bin/sdkman-init.sh"
> EOS

$ sdk version
==== BROADCAST =================================================================
* 14/06/16: Gradle 2.14 released on SDKMAN! #gradle
* 10/06/16: Gradle 2.14-rc-6 released on SDKMAN! #gradle
* 10/06/16: Gradle 3.0-milestone-1 released on SDKMAN! #gradle
================================================================================
SDKMAN 4.0.37
$ sdk install groovy
$ sdk install gradle

Hang tight, this could take a while…

After completing that, run gradle –version to verify that it is correctly installed.

$ groovy -v
Groovy Version: 2.4.7 JVM: 1.8.0_77 Vendor: Oracle Corporation OS: Mac OS X
$ gradle -v
------------------------------------------------------------
Gradle 2.14
------------------------------------------------------------

Build time:   2016-06-14 07:16:37 UTC
Revision:     cba5fea19f1e0c6a00cc904828a6ec4e11739abc

Groovy:       2.4.4
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_77 (Oracle Corporation 25.77-b03)
OS:           Mac OS X 10.11.5 x86_64

Try build sample code

HEAD: 8e2ba4fa5ddeb67da4dbda97172769654210f771

$ git clone https://github.com/carlosvin/cpp_gradle.git
$ cd cpp_gradle

When ran gradle mainReleaseEnterpriseExecutable, I get the following error:

$ gradle mainReleaseEnterpriseExecutable
:compileHelloDebugCommunitySharedLibraryHelloCpp
In file included from /Users/not/cpp_gradle/src/hello/cpp/Hello.cpp:2:
/Users/not/cpp_gradle/src/hello/headers/Hello.h:11:17: error: variable has incomplete type 'class HELLO_API'
class HELLO_API Hello
                ^
/Users/not/cpp_gradle/src/hello/headers/Hello.h:11:7: note: forward declaration of 'HELLO_API'
class HELLO_API Hello
      ^
/Users/not/cpp_gradle/src/hello/headers/Hello.h:11:22: error: expected ';' after top level declarator
class HELLO_API Hello
                     ^
                     ;
/Users/not/cpp_gradle/src/hello/cpp/Hello.cpp:8:1: error: 'Hello' is not a class, namespace, or enumeration
Hello::Hello(const char * who)
^
/Users/not/cpp_gradle/src/hello/headers/Hello.h:11:17: note: 'Hello' declared here
class HELLO_API Hello
                ^
/Users/not/cpp_gradle/src/hello/cpp/Hello.cpp:13:6: error: 'Hello' is not a class, namespace, or enumeration
void Hello::sayHello( unsigned n )
     ^
/Users/not/cpp_gradle/src/hello/headers/Hello.h:11:17: note: 'Hello' declared here
class HELLO_API Hello
                ^
4 errors generated.

:compileHelloDebugCommunitySharedLibraryHelloCpp FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileHelloDebugCommunitySharedLibraryHelloCpp'.
> A build operation failed.
      C++ compiler failed while compiling Hello.cpp.
  See the complete log at: file:///Users/not/cpp_gradle/build/tmp/compileHelloDebugCommunitySharedLibraryHelloCpp/output.txt

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.247 secs

Hello class was declared in src/hello/headers/Hello.h.

class HELLO_API Hello
{

HELLO_API is undefined, so it’s syntax error.

This bug was sent pull request for fix, 10 days ago.
Fixed build for non visual studio environments #8

Umm… Why didn’t check build before merge??

Anyway it fix.

$ git diff
diff --git a/src/hello/headers/Hello.h b/src/hello/headers/Hello.h
index 91133d1..65204cf 100644
--- a/src/hello/headers/Hello.h
+++ b/src/hello/headers/Hello.h
@@ -6,6 +6,8 @@
        #else
                #define HELLO_API __declspec(dllimport)
        #endif
+#else
+       #define HELLO_API
 #endif

 class HELLO_API Hello

Rebuild…

$ gradle mainReleaseEnterpriseExecutable
:compileMainReleaseEnterpriseExecutableMainCpp UP-TO-DATE
:compileHelloReleaseEnterpriseStaticLibraryHelloCpp UP-TO-DATE
:createHelloReleaseEnterpriseStaticLibrary UP-TO-DATE
:helloReleaseEnterpriseStaticLibrary UP-TO-DATE
:linkMainReleaseEnterpriseExecutable UP-TO-DATE
:mainReleaseEnterpriseExecutable UP-TO-DATE

BUILD SUCCESSFUL

Total time: 1.996 secs

Build successful, but it can’t run, because couldn’t load Dynamic Link Library.

$ build/exe/main/release/enterprise/main
dyld: Library not loaded: libhello.dylib
  Referenced from: /Users/not/cpp_gradle/build/exe/main/release/enterprise/main
  Reason: image not found
Trace/BPT trap: 5

Change to the static link.

$ git diff
diff --git a/build.gradle b/build.gradle
index 39f7ee4..f737d8b 100644
--- a/build.gradle
+++ b/build.gradle
@@ -34,8 +34,7 @@ model {
        }
         main(NativeExecutableSpec) {
                binaries.all {
-                       lib library: "hello"
-
+                       lib library: "hello", linkage: "static"
                }
        }
     }
$ build/exe/main/release/enterprise/main
1.  Hello Mr. Pepito    (Enterprise)
2.  Hello Mr. Pepito    (Enterprise)
3.  Hello Mr. Pepito    (Enterprise)
4.  Hello Mr. Pepito    (Enterprise)
5.  Hello Mr. Pepito    (Enterprise)
6.  Hello Mr. Pepito    (Enterprise)
7.  Hello Mr. Pepito    (Enterprise)
8.  Hello Mr. Pepito    (Enterprise)
9.  Hello Mr. Pepito    (Enterprise)
10. Hello Mr. Pepito    (Enterprise)

OK.

Let’s add a new file.

$ git diff -U1
diff --git a/src/hello/cpp/Hello.cpp b/src/hello/cpp/Hello.cpp
index 42c4250..3947893 100644
--- a/src/hello/cpp/Hello.cpp
+++ b/src/hello/cpp/Hello.cpp
@@ -3,2 +3,3 @@
 #include "Msg.h"
+#include "hoge.hpp"
 #include <iostream>
@@ -14,2 +15,3 @@ void Hello::sayHello( unsigned n )
 {
+    cout << ::foo << endl;
        for (unsigned i=0; i<n; i++)
// src/hello/headers/hoge.hpp
#pragma once
extern int foo;
// src/hello/cpp/hoge.cpp
#include "hoge.hpp"
int foo = 100;
$ gradle mainReleaseEnterpriseExecutable
:compileMainReleaseEnterpriseExecutableMainCpp UP-TO-DATE
:compileHelloReleaseEnterpriseStaticLibraryHelloCpp
:createHelloReleaseEnterpriseStaticLibrary
:helloReleaseEnterpriseStaticLibrary
:linkMainReleaseEnterpriseExecutable
:mainReleaseEnterpriseExecutable

BUILD SUCCESSFUL

Total time: 2.287 secs
$ build/exe/main/release/enterprise/main
100
1.  Hello Mr. Pepito    (Enterprise)
2.  Hello Mr. Pepito    (Enterprise)
3.  Hello Mr. Pepito    (Enterprise)
4.  Hello Mr. Pepito    (Enterprise)
5.  Hello Mr. Pepito    (Enterprise)
6.  Hello Mr. Pepito    (Enterprise)
7.  Hello Mr. Pepito    (Enterprise)
8.  Hello Mr. Pepito    (Enterprise)
9.  Hello Mr. Pepito    (Enterprise)
10. Hello Mr. Pepito    (Enterprise)

OK.

In src/hello/cpp/hoge.cpp, rewrite foo‘s value to 1000.

$ gradle mainReleaseEnterpriseExecutable
:compileMainReleaseEnterpriseExecutableMainCpp UP-TO-DATE
:compileHelloReleaseEnterpriseStaticLibraryHelloCpp
:createHelloReleaseEnterpriseStaticLibrary
:helloReleaseEnterpriseStaticLibrary
:linkMainReleaseEnterpriseExecutable
:mainReleaseEnterpriseExecutable

BUILD SUCCESSFUL

Total time: 2.141 secs
$ build/exe/main/release/enterprise/main
1000
1.  Hello Mr. Pepito    (Enterprise)
2.  Hello Mr. Pepito    (Enterprise)
3.  Hello Mr. Pepito    (Enterprise)
4.  Hello Mr. Pepito    (Enterprise)
5.  Hello Mr. Pepito    (Enterprise)
6.  Hello Mr. Pepito    (Enterprise)
7.  Hello Mr. Pepito    (Enterprise)
8.  Hello Mr. Pepito    (Enterprise)
9.  Hello Mr. Pepito    (Enterprise)
10. Hello Mr. Pepito    (Enterprise)

OK.

Properly dependency was processed. It’s very easy!