3

I'm trying to call OpenGL ES 2 functions in some native C++ code for an Android app, built through Android Studio with Gradle.

However, I get linker errors when calling any single OpenGL function, and haven't had much luck figuring out why.

It feels like it should be something as simple as configuring Gradle in some way, but its just not clear how to do so (especially since I have near to no experience with Gradle).

I've created an incredibly simple test case for my problem at the following Github location: https://github.com/NeoSouldier/GLESTest.

Open the project with Android Studio and you will see the code which is failing to link in the "native-lib.cpp" file.

If anyone is able to get this building/linking correctly then please let me know what I'm missing!

Thanks!

2
  • Why don't you try one of official NDK samples, hello-gl2 github.com/googlesamples/android-ndk/tree/master/hello-gl2 you must build using ndk-build so that that cpp files can be made as a static library, and Java will load the library to communicate with it.
    – Sung
    Commented Jan 9, 2017 at 4:41
  • Thank you @Sung, looking at the NDK example led me to solving it!
    – Arthur
    Commented Jan 9, 2017 at 15:54

1 Answer 1

7

Go to your CMakeLists.txt, and add GLESv2 to target link libraries. It should look like this:

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} 
                       GLESv2)
1
  • Thank you Andras - I'd actually figured it from looking at the NDK example that @Sung pointed me to, but not got around to updating the question. Your answer above is exactly how I solved it!
    – Arthur
    Commented Jan 9, 2017 at 15:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.