cocos2d android compile time linking not working - android

I am trying to get cocos2d-android (cocos2d-2.0-rc2-x-2.0.1) "Helloworld" sample to run under windows. I am using latest version of cygwin along side with android ndk r6, android sdk API 8. And I tried the manual here
after a lot of challenges I am down to this problem which I think is in linking the classes at compile time. when I try to run the *build_native.sh* script I get an error stating that in CCGL.h, PFNGLDELETEVERTEXARRAYSOESPROC which is defined as extern, does not name a type.
//declare here while define in CCEGLView_android.cpp
extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT;
extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT;
extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT;
'PFNGLDELETEVERTEXARRAYSOESPROC' is declared in CCEGLView.h.
#if CC_TEXTURE_ATLAS_USE_VAO
#include <EGL/egl.h>
PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;
PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = 0;
PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;
#endif
I tried to add the header address to android.mk under jni directory, to LOCAL_C_INCLUDES, as suggested here,but there were no rules to make them. so can anybody help me with how to compile/link this, it would be much appreciated.

I had this problem, and using a higher version of NDK worked. Try to get the most recent version, reset the environment variables, and run build_native.sh again.
http://developer.android.com/tools/sdk/ndk/index.html

Related

cross compile for android on linux with __ANDROID_API__ predefinition fails my program

I setup conan for cross building android app on my linux, i have my_profile below for conan for this cross building, which that I run conan create . user/testing -pr=my_profile
include(default)
target_host=aarch64-linux-android
android_ndk=$HOME/android-ndk-r21
api_level=21
[settings]
arch=armv8
build_type=Release
compiler=clang
compiler.libcxx=libc++
compiler.version=9
os=Android
os.api_level=$api_level
[build_requires]
[options]
[env]
PATH=[$android_ndk/toolchains/llvm/prebuilt/linux-x86_64/bin]
CHOST=$target_host
AR=$target_host-ar
AS=$target_host-as
LD=$target_host-ld
STRIP=$target_host-strip
RANLIB=$target_host-ranlib
CC=$target_host$api_level-clang
CXX=$target_host$api_level-clang++
CONAN_MAKE_PROGRAM=$android_ndk/prebuilt/linux-x86_64/bin/make
CONAN_CMAKE_TOOLCHAIN_FILE=$android_ndk/build/cmake/android.toolchain.cmake
and I made a very simple file:
#include <fcntl.h>
int raw_fallocate(int fd, off_t length) {
if (fallocate(fd, 0, 0, length) == 0) return 0;
return -1;
}
I found that in the fcntl.h, it only defines fallocate when __ANDROID_API >=21 with #ifdef
so in my CMakeLists.txt, I need to put
target_compile_definitions(hello PRIVATE __ANDROID_API__=21) to make it compile, otherwise, the compiler will complain it cannot find definition of fallocate.
That all make sense. However, when I put this preprocessor definition, I still got a warning message saying:
In file included from <built-in>:413:
<command line>:1:9: warning: '__ANDROID_API__' macro redefined [-Wmacro-redefined]
#define __ANDROID_API__ 21
^
<built-in>:405:9: note: previous definition is here
#define __ANDROID_API__ 16
^
1 warning generated.
What I don't understand is I could not find this built-in thing..., i searched my whole android_ndk folder, and could not find where is this #define __ANDROID_API__ 16
Also I only have android_ndk v21 installed, I have no idea where this version of 16 came from.
Any idea?
This #define __ANDROID_API__ 16 comes from the NDK itself (this is the lowest supported API for android-ndk-r21. To set it to 21, you must pass ANDROID_PLATFORM parameter to CMake. Update: this is actually wrong. For ABI arm64-v8a the minimal API is 21. So, the problem is that arch=armv8 didn't work.
According to the conan instructions, set os.api_level=21 should have worked. But with this approach, you should not supply the CONAN_CMAKE_TOOLCHAIN_FILE. Update: this does not work because conan is not compatible with NDK r21.
I assume that when you do supply CONAN_CMAKE_TOOLCHAIN_FILE, all the settings, like CC= and AR= become irrelevant. Same for os.api_level and arch. But if you replace cmake with a script that calls the original cmake binary and sets the necessarycommand-line parameters, including -DANDROID_PLATFORM=android-21, you should be set. Simply add to my_profile:
CONAN_CMAKE_PROGRAM=cmake-wrapper
This approach is used in https://github.com/bincrafters/conan-android_ndk_installer package.
So I've been digging into this a little bit, and I think I found where the problem is. As #Alex Cohn said, the android ndk sets the min api level to 16 in the file
$android_ndk/build/cmake/platforms.cmake, it says on the first line set(NDK_MIN_PLATFORM_LEVEL "16"). After I change it to 21, everything works.
Now the question becomes: How can I override this value externally without touching this platforms.cmake file (I just don't want to touch files come with ndk package)? I tried to put set(NDK_MIN_PLATFORM_LEVEL "16") in my CMakelists.txt file for my project, but that does not work as I believe it is overridden later on by platforms.cmake.
Of course the second method of using conan's ndk-installer also works, and I checked the same platforms.cmake file in the installed ndk folder, it has the same value of 16. So there must be somewhere in conan settings that updated the value to 21, and I would like to learn where / how this is changed when using ndk-installer, so I can do the same for my manually installled android ndk.

Building an Android executable gRPC server that uses protocol buffers (without APK)

I compiled the gRPC Android example from here.
I want to run the program as executable from adb shell.
Added these lines to grpc-helloworld.cc:
#include <iostream>
int main() {
std::cout << "qwerty" << std::endl;
return 0;
}
And these lines to its CMakeLists.txt:
add_executable(avocado
src/main/cpp/grpc-helloworld.cc)
target_include_directories(avocado
PRIVATE ${HELLOWORLD_PROTO_HEADERS})
target_link_libraries(avocado
helloworld_proto_lib
android
${log-lib})
Then I pushed the generated executable and libs file and tried to run it:
LD_LIBRARY_PATH=. ./avocado
I got the following error:
[libprotobuf FATAL
/home/buga/grpc/third_party/protobuf/src/google/protobuf/stubs/common.cc:79]
This program was compiled against version 3.0.0 of the Protocol Buffer
runtime library, which is not compatible with the installed version
(3.5.1). Contact the program author for an update. If you compiled
the program yourself, make sure that your headers are from the same
version of Protocol Buffers as your link-time library. (Version
verification failed in
"out/soong/.intermediates/frameworks/av/drm/libmediadrm/libmediadrm/android_arm64_armv8-a_kryo300_shared_core/gen/proto/frameworks/av/drm/libmediadrm/protos/plugin_metrics.pb.cc".)terminating
with uncaught exception of type google::protobuf::FatalException: This
program was compiled against version 3.0.0 of the Protocol Buffer
runtime library, which is not compatible with the installed version
(3.5.1). Contact the program author for an update. If you compiled
the program yourself, make sure that your headers are from the same
version of Protocol Buffers as your link-time library. (Version
verification failed in
"out/soong/.intermediates/frameworks/av/drm/libmediadrm/libmediadrm/android_arm64_armv8-a_kryo300_shared_core/gen/proto/frameworks/av/drm/libmediadrm/protos/plugin_metrics.pb.cc".)
Aborted
What am I doing wrong?
We realized that there is a version of the protobuf library called libprotobuf-cpp-full.so and libprotobuf-cpp-lite.so, and it seems that their version is 3.0.0. This is conflicting with our version (3.5.1) which is compiled into either a static lib, or as a shared lib.
I'm not quite sure why this happens. Something about once the linker loads helloworld_proto_lib, it overrides all loaded protobuf symbols, and for some reason another library that you had nothing to do with crashes your program. But that's not telling you anything new.
Here's one way to solve this problem:
1. Changes to grpc-helloworld.cc
Make the main extern "C", and change its name maybe. For example:
extern "C" int my_main() {
std::cout << "qwerty" << std::endl;
return 0;
}
2. Add file grpc-avocado.cc
This will contain the actual main of the executable, which will dynamically load the libraries helloworld_proto_lib and grpc-helloworld. Here's how to do it:
#include <iostream>
#include <android/dlext.h>
#include <dlfcn.h>
int main() {
android_dlextinfo extinfo;
extinfo.flags = ANDROID_DLEXT_FORCE_LOAD;
void* proto_lib = android_dlopen_ext("/path/to/libhelloworld_proto_lib.so", RTLD_LAZY, &extinfo);
void* helloworld = dlopen("/path/to/libgrpc-helloworld.so", RTLD_LAZY);
int (*my_main)() = (int (*)())dlsym(helloworld, "my_main");
return my_main();
}
The function android_dlopen_ext from #include <android/dlext.h>, and its flag argument, are described here: https://developer.android.com/ndk/reference/group/libdl . In the above code we pass the flag ANDROID_DLEXT_FORCE_LOAD, which is documented as:
When set, do not use stat(2) to check if the library has already been loaded.
This flag allows forced loading of the library in the case when for some reason multiple ELF files share the same filename (because the already-loaded library has been removed and overwritten, for example).
Note that if the library has the same DT_SONAME as an old one and some other library has the soname in its DT_NEEDED list, the first one will be used to resolve any dependencies.
I think the text in bold is what explains why this solution works.
3. Change CMakeLists.txt
Since you'll be loading helloworld_proto_lib dynamically, you can now remove it from the executable definition, and there's no need for any proto headers:
add_executable(avocado
src/main/cpp/grpc-avocado.cc)
target_link_libraries(avocado
android
${log-lib})
Build, push, and run
You can now build, push the executable avocado and the two libraries libgrpc-helloworld.so, libhelloworld_proto_lib.so, and run. You don't need LD_LIBRARY_PATH. Good luck with the rest of your project!

Android NDK compiling C++ code gets a Type 'errno_t' could not be resolved

I'm compiling C++ code written primarily for Mac OS, using the Android NDK and I get the following error:
- Type 'errno_t' could not be resolved
In Xcode this type is defined on OSX 10.0/usr/include/sys/_types/_errno_t.h as this:
#ifndef _ERRNO_T
#define _ERRNO_T
typedef int errno_t;
#endif /* _ERRNO_T */
Any suggestions on how to convert this to the NDK, or add compiler flags to make this compile, or where to even get the source code to define this type in my source code itself?
Thanks.
See this answer for information on errno_t.
errno_t is not a part of the C standard, and bionic doesn't support it.
The fix is simply to change all the errno_ts to be ints.

Eclipse ADT "undefined reference to (custom class)", but works fine in Xcode (using Cocos2dx)

I have a project that's fully functional in Xcode but not compiling in Eclipse ADT. I have successfully compiled and run android applications in the past, but this is my first time to create a custom class. To replicate the issue:
I create a new project, then add the following 2 files:
//Enemy.cpp
#include "Enemy.h"
USING_NS_CC;
bool Enemy::init()
{
if(!Layer::init())
return false;
return true;
}
//Enemy.h
#ifndef __ColorMirror__Enemy__
#define __ColorMirror__Enemy__
#include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class Enemy : public cocos2d::Layer{
public:
CREATE_FUNC(Enemy);
virtual bool init();
};
#endif
I am able to see them in Eclipse in the list of classes.
Then I create a new enemy in HelloWorldScene.cpp as follows:
Enemy *newEnemy = Enemy::create();
This compiles and runs as expected in Xcode, but when I run build_native.py I get the following errors:
jni/../../Classes/Enemy.h:19: error: undefined reference to 'Enemy::init()'
jni/../../Classes/Enemy.h:19: error: undefined reference to 'vtable for Enemy'
Checking in the jni/../../Classes file, I am able to find Enemy.h and Enemy.cpp.
I have seen several explanations for fixes in other versions of Eclipse, and the suggestion it's a linker error, but I haven't figured out how to fix it in ADT.
Thanks!
So I'm not entirely sure which of these steps fixed the problem, and it may very well have been multiple issues, but here's what I did:
1) Added Enemy.cpp into Android.mk as follows:
Browse to game folder /proj.android/jni and open Android.mk
Open with TextEdit
Below the AppDelegate.cpp in LOCAL_SRC_FILES: added this:
../../Classes/Enemy.cpp \
Save
(still didn't work, so I took it out)
2) Changed workspace for Eclipse (which is specified on opening eclipse) and deleted old workspace
3) Downloaded and set up newest ADT package (Eclipse JUNO)
4) Upgraded Cocos2d-x from 3.1 to 3.2
(still didn't work)
5) Added Enemy.cpp to Android.mk again
Upon completing all those steps, everything works great and compiles as before.
Thanks to Wez Sie Tato for confirming that Enemy.cpp did in fact need to be added to Android.mk... Based on some comments I've seen elsewhere, it appears that my ADT package may have gotten damaged somehow, in conjunction with not having the proper line in Android.mk

What this error means and how to solve it?

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?

Categories

Resources