Can't remove an empty folder in C++ - android

I want to remove an empty folder using remove() in C++ on Windows 7 but I can't. I tried rmdir() instead of remove() then the folder got removed!
Nevertheless, the reason why I don't use rmdir() is due to Android. In a library project for Android, I can't include "direct.h" header so can't use rmdir(), either. Unlike on Windows, the function remove() works well on Android. I don't understand why.
Anybody knows why this is happening?
Or any other functions which will work on both Windows and Android?

This is a pretty common problem when writing cross-platform programs.
Sometimes, a library can provide the abstraction you need. For example, Boost has a filesystem library that can enumerate files, manipulate directories, etc., on multiple platforms using the exact same code.
Also, there are usually symbols defined that allow you to detect which compiler is currently building your code. Even if there isn't one that does what you want, you can define your own.
Let's say you need to build your software for two different fictitional operating systems named FooOS and for BarOS. I'm going to invent two symbols, FOO_OS and BAR_OS. In my code, I can do something like this:
#ifdef FOO_OS
#include <foo_stuff.h>
#elseif BAR_OS
#include <bar_stuff.h>
#endif
void do_something()
{
#ifdef FOO_OS
do_it_this_way();
#elseif BAR_OS
do_it_that_way();
#endif
}
Now, we just need to either define FOO_OS or BAR_OS. This can be done through an IDE's project configuration or on the command line of the compiler. Use Google to find out about your particular situation, since you didn't include those details in your post.
There is a preprocessing step when you compile your code that makes a pass through the source, and applies these conditional statements. A following pass actually compiles the code. Here is some documentation about Visual Studio's preprocessor, for example.

Related

Tencent NCNN detect function on android modification

The application I use (https://github.com/dangbo/ncnn-mobile.git) use a native library that gives out inference result as a tag. I need it to give me a float array from which it knows the tag. The array is already implemented in the C++ files, but changing them does not effect the application itself. I would not mind if the array was written into a string, I just need the numbers in a readable format. However, the method is native and thus I do not know how to modify this behavior.
I use the newest versions of Android Studio and NCNN. Please advise.
Simply use ndk-build on the jni folder for building.

Listing files/directories in Resources/Assets folder in cocos2dx

I want to list folders and files inside Resources folder from android and ios. I've been able to do so by 'opendir' and 'readdir' from 'dirent.h' but only in ios emulator. It doesn't work in android.
Did anyone face such problem?
Thanks.
AFAIK, this can't be done on Android using only C++. Since you already have the iOS part figured out, you shuould wrap the iOS specific code (#includes too) in
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
...
#endif
The same will aply for Android only code (`CC_PLATFORM_ANDROID') - to achieve what you want you have to write a JNI call in order to call a Java function from C++. Assuming that you want to just dump this data to a log, everything should be pretty straightforward. I don't have a link at hand, but there was an extensive topic about opening URLs which coverd the JNI tutorial and it shouldn't be hard to find.
One way could be to mantain a file containing a list of all the files in the resource directory along with their hierarchy.
For example, say you have a directory hirearchy as:
Resources/
folder1/
file1.jpeg
file2.jpeg
folder2/
file1.jpeg
file2.jpeg
Then you can mantain a file containing the paths as:
in resource_paths.txt
folder1/file1.jpeg
folder1/file2.jpeg
folder2/file1.jpeg
folder2/file2.jpeg
Then search in the file for list of directories/files :)

Wrapping a c++ library using jni

I've successfully added a c++ library to the android 2.3.6 source tree.
Now i want to wrap my library in order to be used from java
I've searched a lot in the net about the steps to do so but almost all the examples i've found are hello world examples and simple ones...
From these examples i've understood that i must create a java file calling the native functions that i need with the prefix native ,then generate the headers and implement the c++ file.
But i don't see how can i call my library functions with this manner.
what is the connection between these headers and my function..?
i am really confused now and i dont know how to start
So, i would be thankfull if someone can tell me the step to wrap a c++ library(already integrated in the aosp)
This is quite a complex problem, and probably beyond the scope of a short answer here. The best thing to do, then, would probably be to guide you to some better documents than the ones you have found so far.
This series of blog posts seems particularly helpful: http://thebreakfastpost.com/2012/01/21/wrapping-a-c-library-with-jni-introduction/
You may also find a wrapper autogenerator helpful. There is one that I haven't tried here: http://www.teamdev.com/jniwrapper/features/
If I understood your problem:
Let's say you have a c++ library with this method
int cPlusPlusMethod{
// Your c++ implementation
}
Basically what you have to do is:
Create the java methods that you need
native int javaMethod();
Follow the steps that you saw in the links you found in order to create a c++ header, you will have something like:
JNIEXPORT jint JNICALL Java_yourPackageName_yourJavaClass_javaMethod(JNIEnv *);
Now you have a c++ header with the declarations of all the methods that you need. Write a c++ file that implements that header and copy-paste the c++ implementation of the library.
JNIEXPORT jint JNICALL Java_yourPackageName_yourJavaClass_javaMethod(JNIEnv *env){
// Your c++ implementation
}
(You will have to include all the needed imports and put the other files of the lib in the 'jni' folder.)
Execute ndk-build from your android project root folder, and that's it.
Maybe this is not the smartest way to do it but at least for me it worked.
Obviously this works if you have the library source files, if you are speaking about a static lib, then I'm not able to help you.

Combining C-code files into one C-code file

I'm converting libx264 to renderscript as an exercise in how much work it is to port a bit larger project into renderscript. One of the pains with renderscript is that everything needs to be declared static to not be automatically getting a java interface. Also this automatic java interface can't handle pointer, multi-dim arrays etc. Hence I need to declare all functions and global variables as static in libx264, besides a few invocation functions to control it.
My problem then is that since everything is declared static I need to have all the code in one file scope. I started to just include all the C-code files into one and compile that. Which would had worked quite easily if not libx264 itself had also included C-files with different pre-processing macro definitions, hence some functions exist twice with different content and some is redeclared identical. I could of course handle this manually, but it would be easier with a tool.
I'm asking if anyone knows of a tool that can take a C project and pre-process/merge that into one C-file, managing re-declarations, conflicting declarations, etc.
And I thought the heap allocations would be the difficult problem...
I have found a tool that does this, CIL.
http://sourceforge.net/projects/cil
http://kerneis.github.com/cil/doc/html/cil/merger.html
/Harald

Will C/C++ libraries work on Android Phone

Does anyone have any experience using JNI to call native C/C++ libraries in Android? Is the environment suitable for running C/C++ libraries and if so is there anything specific about the environment which you need to accommodate?
Thanks
My understanding is that Android provides only a subset of the standard C++ runtime library. For example, Android does not support exceptions in native code. I think there are other restrictions as well.
One complication is that, while Android itself might include many native libraries, only some of them are considered stable enough to link against. The Android NDK page lists the libraries which are safe.
libc (C library) headers
libm (math library) headers
JNI interface headers
libz (Zlib compression) headers
liblog (Android logging) header
A Minimal set of headers for C++ support
If your C library only uses those, you should be fine. C++ support sounds a little spottier.
For a good example of C and Java integration, check this out:
https://github.com/jackpal/Android-Terminal-Emulator
Untar the files and you can find a jni/termExec.cpp - which uses normal C API like "exec()", "fork()", and "open(/dev/ptmx)" to implement terminal emulation (http://linux.die.net/man/4/ptmx).
Looking up the jni/Android.mk file, and u can see that the cpp is compiled as a library - libandroidterm.
And then the java application (src/jackpal/androidterm/Exec.java) will load the library via System.loadLibrary("androidterm").
I think this application provide a small enough example for u to extend whichever way u like - either the cpp or the java file. (The cpp file is basically C-based, not C plus plus).
And remember the mapping between them, for example here it is:
static JNINativeMethod method_table[]
= {
{ "createSubprocess", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[I)Ljava/io/FileDescriptor;",
(void*) android_os_Exec_createSubProcess },
{ "setPtyWindowSize", "(Ljava/io/FileDescriptor;IIII)V",
(void*) android_os_Exec_setPtyWindowSize},
{ "waitFor", "(I)I",
(void*) android_os_Exec_waitFor},
{ "close", "(Ljava/io/FileDescriptor;)V",
(void*) android_os_Exec_close} };
for a C library, you shouldn't have any trouble. a C++ library might be more fun if it uses much of the standard library, because most of the C++ standard library is missing, but you can always supply your own "mini-STL". that's basically how external/webkit works.
much of Android's java.util.regex, java.nio.charset, java.util, and java.text are implemented by calling ICU4C, for example. (the library's in external/icu4c and the JNI is in dalvik/libcore/icu/src/main/native.) a mix of ICU's C and C++ interfaces are used, so you can rest assured this stuff gets quite a good workout on a daily basis ;-)

Categories

Resources