尝试使用gradle构建c ++项目链接到静态库时找不到头文件

时间:2017-07-25 14:56:24

标签: c++ gradle

我想用gradle构建c ++项目,因为我认为它可以成为多语言的一个很好的通用构建工具。我尝试为项目添加一个静态库。 这是目录结构。

.
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── libs
│   └── addlib
│       ├── add.hpp
│       └── libadd.a
└── src
    ├── greeter
    │   ├── cpp
    │   │   └── greeter.cpp
    │   └── headers
    │       └── greeter.hpp
    └── main
        └── cpp
            └── greeting.cpp

这是build.gradle

apply plugin: 'cpp' 

model {
    repositories {
        libs(PrebuiltLibraries) {
            add {
                headers.srcDir "libs/addlib"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile =
                        file("libs/addlib/libadd.a")
                }
            }
        }
    } 
    components {
        greeter(NativeLibrarySpec) {
         } 
        // Let's try using the library
        main(NativeExecutableSpec) { 
            sources {
                cpp.lib library: "greeter" 
            }
        }
    }

    binaries {
        withType(SharedLibraryBinarySpec) {
            if (toolChain in VisualCpp) {
                cppCompiler.define "DLL_EXPORT" 
            }
        }
    }
}

当我执行./gradlew clean mainExecutable时出现错误

> Task :compileMainExecutableMainCpp
/Users/renkai/renkai-lab/building-cpp-libraries/src/main/cpp/greeting.cpp:2:10: fatal error: 'add.hpp' file not found
#include "add.hpp"
         ^
1 error generated.



FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileMainExecutableMainCpp'.
> A build operation failed.
      C++ compiler failed while compiling greeting.cpp.
  See the complete log at: file:///Users/renkai/renkai-lab/building-cpp-libraries/build/tmp/compileMainExecutableMainCpp/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 in 1s
2 actionable tasks: 2 executed

1 个答案:

答案 0 :(得分:0)

我将build.gradle更改为

apply plugin: 'cpp'

model {
    repositories {
        libs(PrebuiltLibraries) {
            myadd {
                headers.srcDir "libs/addlib"
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile =
                        file("libs/addlib/libadd.a")
                }
            }
        }
    }
    components {
        greeter(NativeLibrarySpec) {
         }
        // Let's try using the library
        main(NativeExecutableSpec) {
            sources {
                cpp.lib library: "greeter"
                cpp.lib library: "myadd" , linkage: 'static'
            }
        }
    }

    binaries {
        withType(SharedLibraryBinarySpec) {
            if (toolChain in VisualCpp) {
                cppCompiler.define "DLL_EXPORT"
            }
        }
    }
}

并且构建已通过。我认为add可能是保留的关键字,但gradle没有提供足够的提示。

gradle GitHub存储库中有一些示例:

https://github.com/gradle/gradle/blob/master/subprojects/docs/src/samples/native-binaries/prebuilt/build.gradle