(Android)How to build a native c helloworld with DynamicLinkLibrary? - android

I want to make a native c program with a dynamic link library, so I put these files in Android/development/hello/: hello.c, myprint.h,myprint.c, the main() is in hello.c, and the Android.mk is:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myprint
LOCAL_SRC_FILES := myprint.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := helloworld
LOCAL_SRC_FILES := hello.c
LOCAL_SHARED_LIBRARIES := myprint
include $(BUILD_EXECUTABLE)
Then I make helloworld, and the output info is that:
target thumb C: helloworld <= development/hello/hello.c
target thumb C: myprint <= development/hello/myprint.c
target SharedLib: myprint (out/target/product/generic/obj/SHARED_LIBRARIES/myprint_intermediates/LINKED/myprint.so)
target Symbolic: myprint (out/target/product/generic/symbols/system/lib/myprint.so)
target Strip: myprint (out/target/product/generic/obj/lib/myprint.so)
target Executable: helloworld (out/target/product/generic/obj/EXECUTABLES/helloworld_intermediates/LINKED/helloworld)
/home/neil/dev/google_422/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin/../lib/gcc/arm-linux-androideabi/4.7/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lmyprint.so
development/hello/hello.c:5: error: undefined reference to 'myprint'
collect2: error: ld returned 1 exit status
Please help me...
And my native code is:
// hello.c
#include "myprint.h"
int main()
{
myprint();
return 0;
}
// myprint.h
void myprint();
// myprint.c
#include <stdio.h>
void myprint()
{
printf("myprint...\n");
}

This is happening because you might not be compiling both files so it is unable to locate/link the declaration of that function.
try this
gcc headerfile.c mainfile.c

Related

Building Android Command Line Executable with Prebuilt Shared Library

I'm trying to cross-compile an Android executable (command line) that calls a method inside a proprietary android prebuilt shared object (.so aarch64)
I'm using an Ubuntu 20.04 x86_64 and I want cross-compile an executable for Android 10 aarch64.
The method is defined from the relative JNI in the following way:
public static native int getScore();
I build a simple C program:
#include <stdio.h>
extern int getScore();
int main(){
getScore();
return(1);
}
Then I develop an Android.mk file like the following:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mygame
LOCAL_SRC_FILES := libmygame.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := getscore
LOCAL_SRC_FILES := getscore.c
LOCAL_SHARED_LIBRARIES := mygame
include $(BUILD_EXECUTABLE)
Then I try to compile with:
ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk
But I get the follwoing error:
./obj/local/arm64-v8a/objs/getscore/getscore.o: In function `main':
/tmp/cross-compile-test/./getscore.c:24: undefined reference to `getScore'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/home/mark01/Android/Sdk/ndk/21.3.6528147/build/core/build-binary.mk:738: obj/local/arm64-v8a/getscore] Error 1

Undefined reference error while compiling project (ANDROID NDK)

I am working with the Android NDK and I am facing this problem. I am getting following error;
22:30:09 **** Incremental Build of configuration Default for project MrBlueFramework ****
"D:\\Android_Development\\Android_NDK\\ndk-build.cmd" all
Android NDK: WARNING: APP_PLATFORM android-19 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml
[armeabi] Compile++ thumb: MrBlueFramework <= MrBlueFramework.cpp
[armeabi] SharedLibrary : libMrBlueFramework.so
D:/Android_Development/Android_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/MrBlueFramework/MrBlueFramework.o: in function android_main:jni/MrBlueFramework.cpp:11: error: undefined reference to 'Framework::Application::Application(android_app*)'
D:/Android_Development/Android_NDK/toolchains/arm-linux-androideabi-4.6/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi/objs/MrBlueFramework/MrBlueFramework.o: in function android_main:jni/MrBlueFramework.cpp:11: error: undefined reference to 'Framework::Application::~Application()'
collect2: ld returned 1 exit status
make.exe: *** [obj/local/armeabi/libMrBlueFramework.so] Error 1
Here is what my hierarchy looks like;
Here is my android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MrBlueFramework
LOCAL_SRC_FILES := MrBlueFramework.cpp \
Framework/Application/Application.cpp\
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
Here is content of Application.h
#include <android_native_app_glue.h>
namespace Framework
{
class Application
{
private:
public:
Application(android_app *state);
virtual ~Application();
bool Initialize();
void Run();
};
}
And here is main class (mrblueframework.cpp)
#include <jni.h>
#include <android_native_app_glue.h>
#include "Framework\Application\Application.h"
using namespace Framework;
void android_main(android_app *state)
{
app_dummy();
Application app(state);
}
I don't understand what I am doing wrong, I am including my Application.h file on top but stills it didn't find it. Please help me regarding this.
LOCAL_SRC_FILES := MrBlueFramework.cpp
Application.cpp is not there.

Using minizip with android ndk

I need to use minizip with zlib in android ndk.
My jni/MyApp/Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyApp
LOCAL_SRC_FILES := MyApp.cpp
LOCAL_LDLIBS := -lz
include $(BUILD_SHARED_LIBRARY)
My jni/Android.mk
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
My jni/MyApp/MyApp.cpp
#include <zlib.h>
#include "minizip/unzip.h" #it's in jni/MyApp/minizip/unzip.h
void unzip(char* file, char* folder) {
unzFile zip = unzOpen(file);
}
My jni/Application.mk
APP_MODULES := MyApp
APP_STL := gnustl_static
But, when I compile it:
Gdbserver : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
SharedLibrary : libMyApp.so
C:/android-ndk-r8d/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe:
Z:/workspace/MyApp/obj/local/armeabi/objs-debug/MyApp/MyApp.o: in function unzip(char*, char*):Z:/workspace/MyApp/jni/MyApp/MyApp.cpp:5: error: undefined reference to 'unzOpen' collect2: ld returned 1 exit status make: *** [Z:/workspace/MyApp/obj/local/armeabi/libMyApp.so] Error 1
If I remove the line "unzFile zip = unzOpen(file);" it works
Can you help me? :D
Thanks!
Oh...
I forgot to add the c files in LOCAL_SRC_FILES :D

Link shared library under Android NDK

I with success compile library LibXtract to shared object libxtract.so and want to use is in second project.
In mention project I try to compile it on simple function:
#include <com_androidnative1_NativeClass.h>
#include <android/log.h>
#include "libxtract.h"
JNIEXPORT void JNICALL Java_com_androidnative1_NativeClass_showText
(JNIEnv *env, jclass clazz)
{
float mean = 0, vector[] = {.1, .2, .3, .4, -.5, -.4, -.3, -.2, -.1}, spectrum[10];
int n, N = 9;
float argf[4];
argf[0] = 8000.f;
argf[1] = XTRACT_MAGNITUDE_SPECTRUM;
argf[2] = 0.f;
argf[3] = 0.f;
xtract[XTRACT_MEAN]((void *)&vector, N, 0, (void *)&mean);
__android_log_print(ANDROID_LOG_DEBUG, "LIbXtract", "Button pushe2");
}
I have flat structure:
jni/com_androidnative1_NativeClass.c
jni/com_androidnative1_NativeClass.hjni/libxtract.h
jni/other *.h files from libxtract interface
jni/Android.mk
jni/Applicatoin.mk
library libxtract.so I put in mainproject/lib folder
my Android.mk file looks like:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_androidnative1_NativeClass.c
LOCAL_MODULE := com_androidnative1_NativeClass
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/
LOCAL_LDLIBS += -llog
LOCAL_SHARE_LIBRARIES := libxtract
NDK_MODULE_PATH += $(LOCAL_PATH)/../lib/
include $(BUILD_SHARED_LIBRARY)
and I still got error:
Compile thumb : com_androidnative1_NativeClass <= com_androidnative1_NativeClass.c
SharedLibrary : libcom_androidnative1_NativeClass.so./obj/local/armeabi/objs/com_androidnative1_NativeClass/com_androidnative1_Nativ eClass.o: In function `Java_com_androidnative1_NativeClass_showText':
/home/jack/Projects/AndroidNative1/jni/com_androidnative1_NativeClass.c:20: undefined reference to `xtract'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libcom_androidnative1_NativeClass.so] Error 1
Code came form example of LibXtract and under C++ compile without problems, any ideas?
Your Android make file should be ...
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_SRC_FILES := com_androidnative1_NativeClass.c
LOCAL_MODULE := com_androidnative1_NativeClass
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS += $(LIB_PATH) -lxtract
LOCAL_SHARE_LIBRARIES := libxtract
include $(BUILD_SHARED_LIBRARY)
Try this make file in your second project, and you can successfully build your code without having any error.
In the above answer all is right but exept one.
When we want to link lib we must add -L before LOCAL_LDLIBS dir as below.
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_LDLIBS += **-L**$(LIB_PATH) -lxtract
Else it will give error as below
cannot open XXX/../lib: Permission denied
You need to tell Android NDK build scripts about your shared library. Check ${NDK}/doc/PREBUILTS.html for instructions how this can be done. They advise to add Android.mk in the same directory where you have your libXtract.so:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libXtract
LOCAL_SRC_FILES := libXtract.so
include $(PREBUILT_SHARED_LIBRARY)
Debugging tip: I guess you are using ndk-build to build your "second project". Try running ndk-build with V=99 (try V=99 ndk-build or ndk-build V=99 - my memory failing). This will show you the the exact failing linking command. You should likely have options -lXtract and -L/path/to/libXtract/library. (Sometimes it is convenient to just copy and paste the linking command to run it manually to find the right options for successful linking, before actually fixing the build settings.)
Update: I now see #codetiger's comment seems to point to a same sort of answer (without mentioning the NDK document which is good reading - so I am not deleting this answer).

android build static lib

I am planning to user static Linux .a library into android.
I have created static .a file using following link http://codingfreak.blogspot.in/2010/01/creating-and-using-static-libraries-in.html
i have following Android.mk file in my Android application.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= first-prebuilt
LOCAL_SRC_FILES:= libarith.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := test-main
LOCAL_STATIC_LIBRARIES := first-prebuilt
LOCAL_SRC_FILES := native.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
include $(BUILD_SHARED_LIBRARY)
When I build application using ndk-build r7b, it gives following error.
Сompile++ thumb : test-main <= native.cpp
SharedLibrary : libtest-main.so
/home/hiren/NDK-r7b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: /home/hiren/workspace/VideoTEST/obj/local/armeabi/libarith.a(addition.o): Relocations in generic ELF (EM: 3)
/home/hiren/workspace/VideoTEST/obj/local/armeabi/libarith.a: could not read symbols: File in wrong format
collect2: ld returned 1 exit status
make: *** [/home/hiren/workspace/VideoTEST/obj/local/armeabi/libtest-main.so] Error 1
Can anyone help, I am stuck for a long time, here...
Thanks in advance.
You need to build your static librariy in the android format. See also Error in linking C++ static library with android ndk(Error: file format not recognized)

Categories

Resources