i am trying to get the current OpenGL context on android in c++.
but i get a compile time error, how can i get the current context?
the error:
undefined reference to eglGetCurrentContext()
the code:
#include <GLES2/gl2.h>
#include <EGL/egl.h>
void foo()
{
EGLContext ctx = eglGetCurrentContext();
}
You are missing libEGL from your make file library list.
Assuming you are using CMake files, you need something like this in your make file:
# Include libraries needed
target_link_libraries(
GLESv2
EGL)
Note GLESv2 not needed for this error, but given you include the GLES2 header, it's likely you'll need the GLESv2 library at some point ...
Related
I'm quite new with NDK + Gradle + CMake integration and I'm trying to understand why linking doesn't export symbols as intended.
I have a static library built by a CMakeLists.txt which is not the main CMakeLists.txt.
The scripts does something like:
# main CMakeLists.txt
add_subdirectory(${LIBS}/foo libs}
add_library(native SHARED native.cpp)
# omitting standard android libraries
target_link_libraries(native foo ${android-lib} ${log-lib})
while CMakeLists.txt inside ${libs}/foo is the following:
# misc configuration of ${SRC}
add_library(foo STATIC ${SRC})
The script works fine, it's able to link libnative.so and I'm able to find the generated libfoo.a. Everything seems fine.
I then try to define a native method in foo.cpp contained in foo library:
extern "C" JNIEXPORT void JNICALL Java_com_mypackage_Controls_onTap(JNIEnv*, jobject, int x, int y) {
// log something
}
But I'm not able to call the native method defined in foo library. I get an UnsatisfiedLinkError at runtime. If, instead, I move (directly by copying and pasting) the method to native.cpp then everything goes fine.
So basically:
Java -> method in native.cpp works
Java -> method in native.cpp -> method defined in foo library works
Java -> method in foo library doesn't work (UnsatisfiedLinkError)
I tried to inspect the exported functions with nm and it looks like that foo.a correctly exports the native function as I can see
00011060 T Java_com_mypackage_Controls_onTap
But this entry disappears from libnative.so. If, instead, I define the method directly in native.cpp then I can see it correctly with nm also on libnative.so.
In addition calling any method in foo library from native.cpp works as intended so the library is effectively statically linked.
I am not able to understand the reason behind this, the approach should be fine, visibility should be correct as specified by JNIEXPORT macro so I'm really groping in the dark (and Gradle doesn't provide any output of compilation phase so I can't understand what's happening, but the build.ninja file seems correct)
This behavior, even if unpleasant, is correct. The linker drops any object "files" (in your case, foo.o) from used static libraries, unless they are "pinned" by one of the objects in the shared lib (in your case, native.o). There are three ways to solve the problem:
compile foo.cpp as part of libnative.so instead of a
static lib.
reference Java_com_mypackage_Controls_onTap or any other
external symbol from foo.cpp in native.cpp
use SET(native -Wl,--whole-archive foo -Wl,--no-whole-archive) (see https://stackoverflow.com/a/17477559/192373)
Im trying to build an Ubuntu Touch tree, based on an Aosp tree I already correctly built.
It fails with this error
CAPEWrapper.cpp:16: error: undefined reference to '__xlog_buf_printf'
this is the header that file includes
#include "CAPEWrapper.h"
which on cascade includes
#include <cutils/xlog.h>
which in turn defines
#if defined(__cplusplus)
extern "C" {
#endif
int __xlog_buf_printf(int bufid, const struct xlog_record *rec, ...);
#if defined(__cplusplus)
}
#endif
I suspect that my g++ doesn't set __cplusplus macro. Could it be a realistic scenario with this kind of error? If this could be the problem, should I need to specify a standard implementation with "stdc=something" to solve it?
Any other idea is welcome.
Make sure that your project is linking libcutils, and that it's linking it in the correct order (i.e. that -lcutils appears in the linker command line after any module that depends on it).
In the end, I found that the modules was listed inside a macro called LOCAL_WHOLE_STATIC_LIBRARIES, that in Android environment passes its content to the --whole-archive flag of GCC linker.
I have following commands:
// Load the shaders and get a linked program object
userData->programObject = esLoadProgram( vShaderStr, fShaderStr );
...
// Generate the vertex data
userData->numIndices = esGenCube( 1.0, &userData->vertices,NULL, NULL, &userData->indices );
The program is in native C++ for Android 4, using only NativeActivity. So, project has no /src and java classes.
I put the information to NDK OGLES 2.0 about the version running as EGL_OPENGL_ES2_BIT, and Android.mk was also setup to -lGLESv2. In the file is also included:
#include <GLES/gl.h>
#include <GLES/glext.h>
#include <EGL/egl.h>
#include <GLES2/gl2.h>
And also AndroidManifest was informed that it runs OGLES 2.0.
When asked to run, the program gives the following message:
'esLoadProgram' was not declared in this scope
'esGenCube' was not declared in this scope
For some reason these commands that belong to OGLES 2 are not visible. Any suggestion why this?
All comments are highly appreciated.
esLoadProgram and esGenCube are not a part of OpenGL ES or EGL. They are just a helper functions (probably from http://code.google.com/p/opengles-book-samples/)
PS. I would not suggest to mix also GLES and GLES2 headers. If you want GL ES 2.0, then include only from <GLES2/...> (not <GLES/...>)
I am trying to build a C++ code using NDK in android. I have a method which has a parameter vector < vector <float> > coordinates
Everything builds fine until I write this line inside my method
vector<float> firstPoint = coordinates.at(0);
I start getting this error
D:/eclipseworkspace/myLibProject/obj/local/armeabi/libmyLibProject.a(FileName.o): In function `std::priv::_Vector_base<std::vector<float, std::allocator<float> >, std::allocator<std::vector<float, std::allocator<float> > > >::_M_throw_out_of_range() const':
D:/androidndk/sources/cxx-stl/stlport/stlport/stl/_vector.c:45: undefined reference to `std::__stl_throw_out_of_range(char const*)'
collect2: ld returned 1 exit status
make: *** [/cygdrive/d/eclipseworkspace/myLibProject/obj/local/armeabi/libOutputName.so] Error 1
I have no clue why this is happening and Google is not helping either.
Thanks.
I think you are using two different implementation of the standard library in the same project.
It looks like you are compiling your files with (the headers of) an stlport implementation of the standard library in D:/android..., and you link against your local library.
You have to configure the linker in your ide (or Makefile) to use also the lib file of the same implementation (somewhere in D:/android... I guess).
This is a linking error. You need to add APP_STL := stlport_static to your Apllication.mk file. Also make sure that you use -fno-exceptions flag, since STLport is not compatible with C++ exceptions and RTTI.
You can get more info in APPLICATION-MK.HTML which is availavle in the docs folder of the NDK. CPLUSPLUS-SUPPORT.HTML is also worth to read.
this looks like a linker error. You probably forgot to add STL library reference to your build. Or it can't be found
Did you do this ?
#include <stdexcept>
#include <vector>
using namespace std;
When I changed
vector<float> firstPoint = coordinates.at(0);
to
vector<float> firstPoint = coordinates[0];
it started compiling..... :s y?
Currently I am working with the Android NDK and JNI. I am trying to build a C++ code with NDK.
But I got the following errors:
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:10:19: error: fstream: No such file or directory
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:20: error: 'ifstream' does not name a type
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:21: error: 'ofstream' does not name a type
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:22: error: 'ofstream' does not name a type
E:/Android/Tranining_workspace/BackUpMigrant/jni/ReadBackupArc5/ReadBackupArc5.cpp:34: error: 'string' was not declared in this scope
Can anyone please help me out?
I just encountered the same problem. Seems the STL is not automatically included in NDK projects by default. That also means iostream, fstream, string etc can't be used straight away. To enable them, you'll need to modify your Application.mk file. If you don't have one (it's in the <project>/jni directory), then just create a new, blank one. Add the line:
APP_STL := stlport_static
Also, also remember to include using namespace std; or equivalent, along with the usual #include <iostream> etc.
Did you remember your:
#include <iostream>
using namespace std;
definitions at the top of the file?
("using namespace std" isn't always a good idea, but that's a separate issue.)