如何使用gradle(5.3.1)新的本机插件将外部头文件库包含到gradle native构建中?

问题描述 投票:0回答:1

评估gradle native我想要包含一个只有头文件库{fmt},以便在带有new gradle native plugin的gradle原生多项目中使用。

我试过通过flatDir和maven存储库来包含它。文档仅提及benary依赖项。但即便如此,似乎必须使用build.gradle文件扩展外部代码。下面你看我的最后一次尝试

build.gradle at root

allprojects {
    apply plugin: 'xcode'
    apply plugin: 'visual-studio'

    configurations {
        fmtLib
    }

    dependencies {
        fmtLib files(file("$rootDir/../fmt"))
    }
}

库中的build.gradle将使用{fmt}

plugins {
    id 'cpp-library'
}

library {
    linkage = [Linkage.SHARED]

    targetMachines = [
        machines.windows.x86_64,
        machines.macOS.x86_64,
        machines.linux.x86_64
    ]

    baseName = "greeter"
}

greeter.cpp将使用{fmt}

#define FMT_HEADER_ONLY

#include <iostream>
#include "../public/greeter.hpp"
#include "include/fmt/format.h"

void Greeter::greet() {
    fmt::print("Hello, {}!", "world");
    std::cout << "Hello, " << name << ", your name has " << getNameLength() << " chars." << std::endl;
}

int Greeter::getNameLength() {
    return name.length();
}

上面的示例导致编译错误,这是因为无法解析的依赖关系。

c++ gradle gradle-native
1个回答
0
投票

自己找到了解决方案。不幸的是,gradle在5.3.1之前没有提供更好的解决方案,但可以通过以下方式完成解决方法

def fmtHeaders = file("$rootDir/../fmt/include")

components.main.binaries.whenElementFinalized { binary ->
    project.dependencies {
        if (binary.optimized) {
            add(binary.includePathConfiguration.name, files(fmtHeaders))
        } else {
            add(binary.includePathConfiguration.name, files(fmtHeaders))
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.