其他包括路径和链接器选项Gradle for C++



我试图使用Gradle编译C++项目,但我找不到在编译时声明我想要的附加包含路径,以及在链接时声明哪些附加库的方法。我在项目中看到过涉及model { }的解决方案(我记不清了(。(但没有起作用,Gradle一直抱怨没有现有的功能。(

此外,似乎还不是一个明确或简单的方式添加柯南依赖关系到Gradle项目。这让我只能自己编译库,然后添加到构建系统中,但我还是找不到方法。

我一直在文档中寻找答案,但我发现的只是添加maven依赖项(但我想GLFW、GLEW和DearIMGUI不在maven上…(。

有什么怪癖吗?我怎么可能只注册其他包含/链接文件?

我的build.gradle.kts:

plugins {
id("cpp-application")
}
tasks.register("runDebug") {
doLast {
exec {
executable = "./build/exe/main/debug/my-app.exe"
}
}
}

经过5个小时的搜索,我终于找到了用Gradle构建QT应用程序的解决方案。感谢@s-kadakov。

plugins {
id 'cpp-application'
id 'cpp-unit-test'
}
application {
targetMachines.add(machines.linux.x86_64)
}
tasks.withType(CppCompile).configureEach {
compilerArgs.add '-fPIC'
includes {
'/usr/include/qt'
}
compilerArgs.addAll toolChain.map { toolChain ->
if (toolChain in [ Gcc, Clang ]) {
return ['-O2', '-fno-access-control']
} else if (toolChain in VisualCpp) {
return ['/Zi']
}
return []
}
}
tasks.withType(LinkExecutable).configureEach {
linkerArgs.add '-v'
linkerArgs.add '-lQt5Core'
linkerArgs.add '-lQt5Widgets'
linkerArgs.add '-lQt5Gui'
}

我希望这对你有帮助。

使用Gradle 6.7,您可以执行以下操作:

/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample C++ project to get you started.
* For more details take a look at the Building C++ applications and libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.7/userguide/building_cpp_projects.html
*/
plugins {
// Apply the cpp-application plugin to add support for building C++ executables
id 'cpp-application'
}
// Set the target operating system and architecture for this application
application {
targetMachines.add(machines.linux.x86_64)

// You may add extra private headrs 
//privateHeaders {
//  from('src/headers')
//}    
}
// Some compiler configuration
tasks.withType(CppCompile).configureEach {
// Define a preprocessor macro for every binary
macros.put("NDEBUG", null)
// Define a compiler options
compilerArgs.add '-W3'
// You may add extra include path 
// as of -I<path> option 
//compilerArgs.add '-I/usr/local/include/foo'

// ... or even as list of extra paths
includes {
'/usr/local/include/foo'
}
// Define toolchain-specific compiler options
compilerArgs.addAll toolChain.map { toolChain ->
if (toolChain in [ Gcc, Clang ]) {
return ['-O2', '-fno-access-control']
} else if (toolChain in VisualCpp) {
return ['/Zi']
}
return []
}
}
// Linker configuration
tasks.withType(LinkExecutable).configureEach {
// Add verbose output to linker
// usefull while errors
linkerArgs.add '-v' 

// Add additional libraries as simple as
linkerArgs.add '-lm' 
linkerArgs.add '-lfoo' 
}

添加头目录或添加编译器选项;在插件部分之后添加一个应用程序部分,如下所示:

application {
privateHeaders {
from('src/headers')
from('/usr/include/qt5')
}
// To add compiler args do the following below
binaries.configureEach {
compileTask.get().compilerArgs.add('-O2')
}
}

我仍在尝试如何添加库或链接器参数:(

使用Gradle 7.4.1,这适用于我的cpp库:

apply plugin: 'cpp-library'
...
library {
...
binaries.configureEach {
...
linkTask.get().linkerArgs.add("<argument here>")
...
}
}

最新更新