Gradle Cpp setup with Glm and Glfw as library projects

Hi guys,

I have been trying to setup for a couple of days a new project using the cpp gradle (2.9) plugin with GLM and GLFW as pre-built library projects but have not managed yet. I am not really familiar myself with this libraries, so maybe has already experience including them in a project.

This is my build.gradle file:

model {
    repositories {
        libs(PrebuiltLibraries) {
            glfw {
                headers.srcDir "libs/glfw-3.1.2/include"
            glm {
                // There no convenient way to include headers recursively (this seems to be a bug?)
                //headers.srcDir "libs/glm/glm/**/*.hpp"
                headers.srcDir "lib/glm"

// Executables
model {
    components {
        main(NativeExecutableSpec) {
            // Source-library
            sources {
                cpp {
                  source {
                    srcDir "src/"

                  lib library: "glfw", linkage: "api"
                  lib library: "glm", linkage: "api"

// All-binaries
model {
    binaries {
        all {
            // Define a preprocessor macro for every binary
            cppCompiler.define "NDEBUG"

            // Define toolchain-specific compiler and linker options
            if (toolChain in Gcc) {
                cppCompiler.args "-O2", "-fno-access-control"
                linker.args "-Xlinker", "-S"
            if (toolChain in VisualCpp) {
                cppCompiler.args "/Zi"
                linker.args "/DEBUG"

My problem is that when I try to compile the application, does not manage to find glm headers. The application using CMake is just working fine.

/Users/javier-tarazaga/Development/personal/engine-POC/superName/sdk/engine/src/renderer.cpp:2:10: fatal error: 'glm/gtc/matrix_transform.hpp' file not found
#include <glm/gtc/matrix_transform.hpp>
1 error generated.

:compileMainExecutableMainCpp FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileMainExecutableMainCpp'.
> A build operation failed.
      C++ compiler failed while compiling renderer.cpp.


What does your project layout look like? Is the matrix_transform.hpp header in lib/glm/glm/gtc/?

Had it like that before (which didn’t work either) and then I changed it lib/glm/gtc. The structure is the following:

app -> libs -> glm -> gtc -> matrix_transform.hpp
app -> src -> renderer.cpp

The path you set for headers.srcDir is basically the path we’ll use as an include path for compilation (e.g. -I{somePath}). So if your source includes files like #include <glm/gtc/matrix_transform.hpp> and the header file is in libs/glm/gtc/, you would need to set headers.srcDir to libs/.

You probably want to have the headers moved up a bit in libs/glm/glm/gtc/... (note the second glm), so you can set headers.srcDir to libs/glm/ instead.

You can check the command-line sent to the compiler by looking at the options.txt file under build/tmp

Ok great! That actually solved it :slight_smile:

One problem less. Now the complicated part. GLFW

Undefined symbols for architecture x86_64:
  "_glAttachShader", referenced from:
  "_glBindBuffer", referenced from:
      Renderer::render() in renderer.o
  "_glBindTexture", referenced from:
      Texture::load(String const&) in texture.o
      Texture::use() in texture.o
  "_glBlendFunc", referenced from:

Obviously is not linking the lib. According to GLFW:

Once GLFW has been added to the project, the GLFW_LIBRARIES cache variable contains all link-time dependencies of GLFW as it is currently configured. To link against GLFW, link against them and the glfw target.

This is the simple CMake commands needed to be done correctly:
target_link_libraries(engine glfw ${GLFW_LIBRARIES})`

The question is that I have no idea how transform all this into Gradle. 

Thanks sterling for the help btw

Since Gradle doesn’t yet understand pkgconfig and the like, you have to specify where to find the libraries (here’s a sample that’s included in the Gradle -all distribution):