sysinfo function porting to ndk

844 views
Skip to first unread message

C Alert

unread,
May 19, 2010, 3:44:18 AM5/19/10
to android-ndk
Hi all,
I'm porting Linux C code to NDK. I was used sysinfo function in my
code.

When i compile the souce code , i get the following error.

undefined reference to `sysinfo'
collect2: ld returned 1 exit status

But still the problem is happen.

herewtih i attached the sample code.

Please guide me, how to resolve this issue.

=======Compile Error========
make APP=sysinfo
Android NDK: Building for application 'sysinfo'
Compile thumb : sysinfo <= apps/sysinfo/project/jni/sysinfo.c
Executable : sysinfo
out/apps/sysinfo/armeabi/objs/sysinfo/sysinfo.o: In function `main':
../android-ndk-r3/apps/sysinfo/project/jni/sysinfo.c:46: undefined
reference to `sysinfo'
collect2: ld returned 1 exit status
make: *** [out/apps/sysinfo/armeabi/sysinfo] Error 1

=========================


======Source Code: sysinfo.c =========
#include <sys/sysinfo.h>
#include <stdio.h>

int main() {

int days, hours, mins;
struct sysinfo sys_info;

if(sysinfo(&sys_info) != 0)
perror("sysinfo");

// Uptime
days = sys_info.uptime / 86400;
hours = (sys_info.uptime / 3600) - (days * 24);
mins = (sys_info.uptime / 60) - (days * 1440) - (hours * 60);

printf("Uptime: %ddays, %dhours, %dminutes, %ldseconds\n",
days, hours, mins, sys_info.uptime % 60);

// Load Averages for 1,5 and 15 minutes
printf("Load Avgs: 1min(%ld) 5min(%ld) 15min(%ld)\n",
sys_info.loads[0], sys_info.loads[1], sys_info.loads[2]);

// Total and free ram.
printf("Total Ram: %ldk\tFree: %ldk\n", sys_info.totalram / 1024,
sys_info.freeram / 1024);

// Shared and buffered ram.
printf("Shared Ram: %ldk\n", sys_info.sharedram / 1024);
printf("Buffered Ram: %ldk\n", sys_info.bufferram / 1024);

// Swap space
printf("Total Swap: %ldk\tFree: %ldk\n", sys_info.totalswap / 1024,
sys_info.freeswap / 1024);

// Number of processes currently running.
printf("Number of processes: %d\n", sys_info.procs);

return 0;
}


========================

Thanks

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

David Turner

unread,
May 19, 2010, 2:48:13 PM5/19/10
to andro...@googlegroups.com
Ah, it's an oversight. While it is defined in a public header, the sysinfo() implementation is not part of any C library.
Given that the declaration it perfectly ok, you can roll your own implementation if you need this.
For example, put the following in a .S file and add it to your project:

------------- cut here ---------------------
#include <sys/linux-syscalls.h>

    .text
    .type sysinfo, #function
    .globl sysinfo
    .align 4
    .fnstart

sysinfo:
    .save   {r4, r7}
    stmfd   sp!, {r4, r7}
    ldr     r7, =__NR_sysinfo
    swi     #0
    ldmfd   sp!, {r4, r7}
    movs    r0, r0
    bxpl    lr
    b       __set_syscall_errno
    .fnend
------------ end of code ----------------------

C Alert

unread,
May 19, 2010, 9:22:16 PM5/19/10
to android-ndk
Thanks for your reply and information.

Now its working well.
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr­oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/android-ndk?hl=en.- Hide quoted text -
>
> - Show quoted text -

EnricoC

unread,
May 20, 2010, 1:03:26 PM5/20/10
to android-ndk
Hi David

Thanks for the advice, but I've tried it in my project (which uses
sysinfo()) and the result was

undefined reference to `__NR_sysinfo'

(I've added a file to the project named sysinfo.S with all your code,
and built within the Android.mk)

Do you know why?

Thanks in advance for your help
Enrico

On May 19, 1:48 pm, David Turner <di...@android.com> wrote:
> Ah, it's an oversight. While it is defined in a public header, the sysinfo()
> implementation is not part of any C library.
> Given that the declaration it perfectly ok, you can roll your own
> implementation if you need this.
> For example, put the following in a .S file and add it to your project:
>
> ------------- cut here ---------------------
> #include <sys/linux-syscalls.h>
>
>     .text
>     .type sysinfo, #function
>     .globl sysinfo
>     .align 4
>     .fnstart
>
> sysinfo:
>     .save   {r4, r7}
>     stmfd   sp!, {r4, r7}
>     ldr     r7, =__NR_sysinfo
>     swi     #0
>     ldmfd   sp!, {r4, r7}
>     movs    r0, r0
>     bxpl    lr
>     b       __set_syscall_errno
>     .fnend
> ------------ end of code ----------------------
>
> > android-ndk...@googlegroups.com<android-ndk%2Bunsubscribe@googlegr oups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/android-ndk?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "android-ndk" group.
> To post to this group, send email to andro...@googlegroups.com.
> To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/android-ndk?hl=en.

David Turner

unread,
May 20, 2010, 1:32:07 PM5/20/10
to andro...@googlegroups.com
Ah yes, sorry, you should #define __NR_sysinfo 116, this is the official number for the ARM kernel.

EnricoC

unread,
May 20, 2010, 1:39:00 PM5/20/10
to android-ndk
Great, now it works smoothly!

Thank you very much David, I appreciate that ;-)

Cheers,
Enrico

Prabhu Konchada

unread,
May 7, 2015, 5:48:33 AM5/7/15
to andro...@googlegroups.com, di...@android.com
I have added this file to the jni folder under java folder and also updated the Android.mk file which looks like this

This is my android.mk file

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := CalcNative.c \
sysinfo_implementation.s (THE NAME I GAVE TO YOUR FILE)
LOCAL_MODULE := CalcNative
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

sysinfo_implementation.s is your code snippet

but still my issue not resolved please help with the same.

Prabhu Konchada

unread,
May 7, 2015, 5:48:35 AM5/7/15
to andro...@googlegroups.com
Can you please tell me where did you add it and how did you build it ???

Prabhu Konchada

unread,
May 7, 2015, 10:29:21 AM5/7/15
to andro...@googlegroups.com, di...@android.com
YEAH I HAVE ADDED THIS  TO MAKE THE FILE LOOK LIKE


#define __NR_sysinfo 116

#include <sys/linux-syscalls.h>

.text
.type sysinfo, #function
.globl sysinfo
.align 4
.fnstart

sysinfo:
.save {r4, r7}
stmfd sp!, {r4, r7}
ldr r7, =__NR_sysinfo
swi #0
ldmfd sp!, {r4, r7}
movs r0, r0
bxpl lr
b __set_syscall_errno
.fnend

I GET THIS :

Prabhus-MBP:main prabhu$ ndk-build NDK_PROJECT_PATH=$(pwd) APP_BUILD_SCRIPT=$(pwd)/jni/Android.mk
[armeabi] SharedLibrary  : libCalcNative.so
/Users/prabhu/AndroidStudioProjects/MathApplication/app/src/main/jni/sysinfo_implementation.s:13: error: undefined reference to '__NR_sysinfo'
collect2: error: ld returned 1 exit status
make: *** [/Users/prabhu/AndroidStudioProjects/MathApplication/app/src/main/obj/local/armeabi/libCalcNative.so] Error 1

Prabhu Konchada

unread,
May 7, 2015, 10:29:22 AM5/7/15
to andro...@googlegroups.com, di...@android.com
Can you please tell me how to add and to port it in android.mk Thanks in advance ....
Reply all
Reply to author
Forward
0 new messages