'Additional include paths and linker options Gradle for C++
I was trying to use Gradle to compile a C++ project, but I can't find the way of declaring what additional include paths I want at compile time, and what additional libraries at link time.
I've seen solutions involving model { } inside project (I don't remember exactly). (But didn't work, Gradle complains all the time about non existing function.)
Also does not seem to be a clear or simple way to add Conan dependencies to a Gradle project yet. That leaves me compiling libraries on my own and then adding to the build system, but again, I can't find the way.
I've been looking in the docs for the answer, but all I find is about adding maven dependencies (But GLFW, GLEW and DearIMGUI aren't on Maven I guess...).
Any quirk? How Can I possibly just register additional includes/link files?
My build.gradle.kts:
plugins {
id("cpp-application")
}
tasks.register("runDebug") {
doLast {
exec {
executable = "./build/exe/main/debug/my-app.exe"
}
}
}
Solution 1:[1]
After 5 hours of searching, I finally found a solution to build QT application with Gradle. Thanks to @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'
}
I hope this helps for you.
Solution 2:[2]
Using Gradle 6.7 you may do something like that:
/*
* 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'
}
Solution 3:[3]
To add header directories or add compiler options; after the plugins section add an application section like this:
application {
privateHeaders {
from('src/headers')
from('/usr/include/qt5')
}
// To add compiler args do the following below
binaries.configureEach {
compileTask.get().compilerArgs.add('-O2')
}
}
I am still trying to figure how to add the libraries or linker args :(
Solution 4:[4]
Using Gradle 7.4.1, this worked for my cpp-library:
apply plugin: 'cpp-library'
...
library {
...
binaries.configureEach {
...
linkTask.get().linkerArgs.add("<argument here>")
...
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rejedai |
| Solution 2 | S. Kadakov |
| Solution 3 | Don Wagner |
| Solution 4 | el_chupacabra |
