当我包含 header 时,Gradle 不会构建可执行文件

Gradle will not build an executable when I include a header

我正在使用 gradle 作为本机二进制构建工具进行实验,我遇到了一个我似乎无法弄清楚的最奇怪的问题。如果我从 gradle.build 中删除 include 目录,我会得到一个很好的 exe(当然注释掉方法调用),但是当我包含 header 目录时,它说它构建良好没有问题,但我没有可执行文件了。我尝试了几种不同的设置,但似乎每个人都说这应该可行。外部库加载正常。它的行为就好像它只是不想使用 include 进行编译。

main.cpp

#include <iostream>

#include "Chpt_1.h"

int main(){
    std::cout << "Hello, world!" << std::endl;

    Chpt_1.count();
    return 0;
}

Chat_1.h

#ifndef CHPT_1_H
#define CHPT_1_H

#include <iostream>

int count() {
    std::cout << "Count!" << std::endl;

    return 0;
}

#endif

build.gradle:

apply plugin: 'cpp'

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

目录结构

Shards

build.gradle
[other misc files]
-src
--main
---cpp
----main.cpp
---headers
----Chpt_1.h

很晚才回答,但也许对其他人有帮助。

您的 gradle.build 应该在 "sources" 部分指定源文件。我找不到定义单独的 "include" 目录的方法。但是您尝试的方式并没有按照预期的方式进行。 将 sources 更改为

sources {
    cpp {
        source {
            srcDir "src/main/cpp"
            include "**/*.cpp"
        }
    }
}

Gradle只会编译src/main/cpp.

中的cpp文件

所有包含文件必须相对于源目录导入。例如

#include "../includes/Chpt_1.h"

或者您将包含文件复制到同一目录。