How does one set a C/C++ include path with Gradle Experimental Plugin?
Asked Answered
H

4

6

Android Studio cannot find my header files when they are in a location different to the main source folder of my module. This is noticeable by #include "SDL_config.h" statements being highlighted in red (other #include statements are fine).

I have tried modifying the cppFlags values below but I am starting to doubt that these cppFlags are even being passed to the compiler at all.

Has anyone managed to include files from folders other than their main source folder in this way?

Is there a problem with the way I have specified the cppFlags, or perhaps the moduleName or even sources?

I could go through the SDL2 source and modify all of the #include statements to relative #include paths but I don't really want to modify the SDL2 source. I should simply be able to specify a list of include paths somehow.

apply plugin: 'com.android.model.application'

model {
...
    android.ndk {
        moduleName = 'main'

        //W:\hello-sdl-android\android-project\app\src\main\jni\SDL2\include
        cppFlags += "-I${file("src/main/jni/SDL2/include")}".toString()
        cppFlags += "-I${file("src/main/jni/SDL2/src")}".toString()
    }

    android.sources {
        main.jni {
            source {
                srcDirs += ['src/main/jni/src']
                srcDirs += ['src/main/jni/SDL2/src']
            }
        }
    }
...
}

UPDATE: Here's more information on my current project layout:

app/src/main/jni app/src/main/jni/src/Main.cpp <- This is compiling app/src/main/jni/SDL2 <- All SDL2 source is under here app/src/main/jni/GLM <- All GLM source is under here

This layout is a direct result of using the example project which I was provided here: https://gitlab.com/scannerdarkly/hello-sdl-android

That project will build using ndk-build from the command line - I wanted to take things a step further by building within Android Studio. My project will attempt to draw a triangle on a GLES2 device.

Here is a link to my current project so far:

http://www.mediafire.com/download/92577p7vf123l72/hello-sdl-android.zip

Hindgut answered 11/11, 2015 at 19:53 Comment(0)
H
2

Yes! I figured it out - SDL2 source files are .C files, so include paths need to be declared using CFlags, not cppFlags.

Hindgut answered 12/11, 2015 at 0:33 Comment(0)
G
9

I use a slightly different approach:

cFlags += "-I" + file("src/main/jni/SDL2/include").absolutePath

.. and this works. The reason is probably that the compiler is launched with a different working directory, and absolutePath resolves any ambiguity here.

Gothicize answered 11/11, 2015 at 21:8 Comment(5)
Thanks - I will give this a try in a couple of hours and let you know if that helps. I will delete the other question if this is the answer.Hindgut
That still isn't working for me. Actually, I have just tried placing the offending #include "SDL_config.h" into my main.cpp file and it appears to be picking it up fine. Its when I am compiling the SDL2 source that I have a problem.. So do I need an additional "moduleName" or some form of "sub" module name perhaps? I am confused as to what "moduleName" actually refers to anyway - surely it means an executable or library - not an individual .cpp file. I will provide a download link shortly. Getting this to work will benefit others.Hindgut
One more thing - SDL source files are C files.. Main.cpp is a C++ file. Maybe my problem is as simple as declaring "cFlags" in addition to "cppFlags"?Hindgut
cFlags effect C++ files too, so you don't need two settings for your projectGothicize
@AlexCohn In my case CFlags did not affect to c++ files and I set headers for cppFlags tooInsectivore
H
2

Yes! I figured it out - SDL2 source files are .C files, so include paths need to be declared using CFlags, not cppFlags.

Hindgut answered 12/11, 2015 at 0:33 Comment(0)
V
1

Here is another style for more header paths using the gradle experimental plugin, example with openssl and some "abc" library:

// compile parameters

// include openssl headers C (if you have C files)
CFlags.add("-isystem${project.rootDir}/external-libraries/openssl/openssl-1.0.2g/include".toString())

// include openssl headers C++ (if you have cpp files)
cppFlags.add("-isystem${project.rootDir}/external-libraries/openssl/openssl-1.0.2g/include".toString())

// include abc headers C (if you have C files)
CFlags.add("-I${project.rootDir}/external-libraries/abc/abc-5.5/include".toString())

// include abc headers C++ (if you have cpp files)
cppFlags.add("-I${project.rootDir}/external-libraries/abc/abc-5.5/include".toString())

// linking parameters

// link libcrypto.so
ldFlags.add("-L${project.rootDir}/external-libraries/openssl/openssl-1.0.2g/lib/armeabi-v7a".toString())
ldLibs.add('crypto')

// link libabc.so
ldFlags.add("-L${project.rootDir}/external-libraries/abc/abc-5.5/lib".toString())
ldLibs.add('abc')
Varien answered 20/9, 2016 at 14:5 Comment(0)
R
0

for many header paths :

cFlags = "-I" + file("src/main/jni/path1").absolutePath +
     " -I" + file("src/main/jni/path2").absolutePath +
     " -I" + file("src/main/jni/path3").absolutePath
Roller answered 12/4, 2016 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.