带有 gradle 的 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21059516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ with gradle
提问by Paul Verest
In Gradle 1.10 Release notes http://www.gradle.org/docs/current/release-notesI see C++ build mentioned.
在 Gradle 1.10 发行说明http://www.gradle.org/docs/current/release-notes 中,我看到提到了 C++ 构建。
How to set up C++ project to build with gradle?(without IDE)
如何设置 C++ 项目以使用 gradle 构建?(没有IDE)
Suppose I have
假设我有
ProjectFolder/hello.cpp
ProjectFolder/build.gradle
hello.cpp
:
hello.cpp
:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
puts("Hello World!!!");
return EXIT_SUCCESS;
}
What should basic build.gradle
befor this C++ project?
build.gradle
这个 C++ 项目的基础应该是什么?
UPDATE: I have already looked at Chapter 72 of User Guideand 2 year old examples mentioned. They don't make it simpler but more complicated.
更新:我已经看过用户指南的第 72 章和提到的 2 年前的例子。他们没有让它更简单,而是更复杂。
There is 1 file example with 6 lines. I haven't been touching C++ for 10 year and I just wanted quick start e.g. with GCC . (Not yet found)
有 1 个文件示例,有 6 行。我已经 10 年没有接触过 C++,我只是想快速开始,例如使用 GCC。(还没找到)
回答by Perryn Fowler
put this in build.gradle
apply plugin: 'cpp' executables { hello {} }
put your source file in src/hello/cpp/say_hello.cpp
run 'gradle helloExecutable'
your executable should be built to build/binaries/helloExecutable/hello
把它放在 build.gradle 中
apply plugin: 'cpp' executables { hello {} }
把你的源文件放在 src/hello/cpp/say_hello.cpp
运行'gradle helloExecutable'
您的可执行文件应该构建为 build/binaries/helloExecutable/hello
Or, if you want your source in src/foo/bar then add
或者,如果您希望源代码位于 src/foo/bar 中,则添加
sources {
hello {
cpp {
source {
srcDir "src/foo/bar"
}
}
}
回答by Amnon Shochot
Starting on Gradle 2.3 there have been major changes to native-component builds and the executables
and libraries
containers are not available anymore. Citing Gradle 2.3 Release notes:
从 Gradle 2.3 开始,本机组件构建发生了重大变化,executables
并且libraries
容器不再可用。引用Gradle 2.3 发行说明:
... the DSL for defining native executables and libraries has fundamentally changed. The executables and libraries containers have been removed, and components are now added by type to the components container owned by the model registry. Another major change is that source sets for a component are now declared directly within the component definition, instead of being configured on the sources block.
...用于定义本机可执行文件和库的 DSL 已经发生了根本性的变化。可执行文件和库容器已被删除,组件现在按类型添加到模型注册表拥有的组件容器中。另一个主要变化是组件的源集现在直接在组件定义中声明,而不是在源块上配置。
The updated Gradle code compatible with Gradle 2.3+ will therefore look like:
因此,与 Gradle 2.3+ 兼容的更新后的 Gradle 代码将如下所示:
model {
components {
hello(NativeExecutableSpec) {
sources {
cpp {
source {
srcDir "src/foo/bar"
}
}
}
}
}
}
You can learn more about the new model in Gradle user guide here.
您可以在此处的Gradle 用户指南中了解有关新模型的更多信息。
回答by user3509406
This is another answer. I am using Gradle 2.4.7. My source code is the standard C++ as in the following directory structure on Windows:
这是另一个答案。我正在使用 Gradle 2.4.7。我的源代码是标准的 C++,如 Windows 上的以下目录结构:
myproject
我的项目
myproject\src
我的项目\src
myproject\src\main.cpp
我的项目\src\main.cpp
myproject\build.gradle
我的项目\build.gradle
main.cpp is just a Hello world C++ program.
main.cpp 只是一个 Hello world C++ 程序。
I have Visual Studio 2015. I like to use its C++ compiler for the standard C++ programs.
我有 Visual Studio 2015。我喜欢将其 C++ 编译器用于标准 C++ 程序。
build.gradle is as follows:
build.gradle 如下:
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.
额外的编译器选项和链接器选项是由于 Visual Studio 2015 与其先前版本相比发生了一些变化。如果没有这些选项,您将获得无法找到 corecrt.h 的编译或无法找到 libucrt.lib 的链接器错误。
Hope this will help you to begin C++ compilation with Visual Studio 2015 quickly!!!
希望这能帮助您快速开始使用 Visual Studio 2015 进行 C++ 编译!!!