2013-03-20 149 views
0

我試着去編譯reboot.c二進制爲Android,但我不斷收到以下錯誤:LD返回退出狀態1 - ç

/home/pedja/android-ndk-r8d/toolchains/x86-4.6/prebuilt/linux-x86/bin/../lib/gcc/i686- linux-android/4.6/../../../../i686-linux-android/bin/ld: ./obj/local/x86/objs/reboot /reboot.o: in function main:jni/reboot.c:49: error: undefined reference to 'android_reboot' 
/home/pedja/android-ndk-r8d/toolchains/x86-4.6/prebuilt/linux-x86/bin/../lib/gcc/i686-linux-android/4.6/../../../../i686-linux-android/bin/ld: ./obj/local/x86/objs/reboot /reboot.o: in function main:jni/reboot.c:51: error: undefined reference to 'android_reboot' 
/home/pedja/android-ndk-r8d/toolchains/x86-4.6/prebuilt/linux-x86/bin/../lib/gcc/i686-linux-android/4.6/../../../../i686-linux-android/bin/ld: ./obj/local/x86/objs/reboot/reboot.o: in function main:jni/reboot.c:47: error: undefined reference to 'android_reboot' 
collect2: ld returned 1 exit status 
make: *** [obj/local/x86/reboot] Error 1 

reboot.c

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include "android_reboot.h" 
#include <unistd.h> 

int main(int argc, char *argv[]) 
{ 
    int ret; 
    int nosync = 0; 
    int poweroff = 0; 
    int flags = 0; 

    opterr = 0; 
    do { 
     int c; 

     c = getopt(argc, argv, "np"); 

     if (c == EOF) { 
      break; 
     } 

     switch (c) { 
     case 'n': 
      nosync = 1; 
      break; 
     case 'p': 
      poweroff = 1; 
      break; 
     case '?': 
      fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n", argv[0]); 
      exit(EXIT_FAILURE); 
     } 
    } while (1); 

    if(argc > optind + 1) { 
     fprintf(stderr, "%s: too many arguments\n", argv[0]); 
     exit(EXIT_FAILURE); 
    } 

    if(nosync) 
     /* also set NO_REMOUNT_RO as remount ro includes an implicit sync */ 
     flags = ANDROID_RB_FLAG_NO_SYNC | ANDROID_RB_FLAG_NO_REMOUNT_RO; 

    if(poweroff) 
     ret = android_reboot(ANDROID_RB_POWEROFF, flags, 0); 
    else if(argc > optind) 
     ret = android_reboot(ANDROID_RB_RESTART2, flags, argv[optind]); 
    else 
     ret = android_reboot(ANDROID_RB_RESTART, flags, 0); 
    if(ret < 0) { 
     perror("reboot"); 
     exit(EXIT_FAILURE); 
    } 
    fprintf(stderr, "reboot returned\n"); 
    return 0; 
} 

android_reboot.h

#ifndef __ANDROID_REBOOT_H__ 
#define __ANDROID_REBOOT_H__ 

__BEGIN_DECLS 

/* Commands */ 
#define ANDROID_RB_RESTART 0xDEAD0001 
#define ANDROID_RB_POWEROFF 0xDEAD0002 
#define ANDROID_RB_RESTART2 0xDEAD0003 

/* Flags */ 
#define ANDROID_RB_FLAG_NO_SYNC  0x1 
#define ANDROID_RB_FLAG_NO_REMOUNT_RO 0x2 

int android_reboot(int cmd, int flags, char *arg); 

__END_DECLS 

#endif 

回答

1

您顯示您已聲明android_reboot但是您的鏈接失敗,因爲該函數的實現不能 被發現。聲明與定義不同......聲明滿足編譯器,但只有實現滿足鏈接器。這個功能在哪裏實施?

+0

我不知道,我不知道任何關於c。我從android源代碼中獲得了這個,我需要它用於我的java應用程序 – pedja 2013-03-20 11:23:45

+1

您從這個源代碼獲得的源代碼包含了'android_reboot'的其他源代碼(需要引入),或者說明了要包含在鏈接中的庫您需要添加到您的構建腳本中),或者不完整。 – mah 2013-03-20 11:25:14

+0

我可能要求很多,但是你能否指出我正確的方向。這是來源:https://android.googlesource.com/platform/system/core.git/+/master/toolbox/我真的不想從頭開始學習c,只是因爲我不得不編譯很少的二進制文件在c – pedja 2013-03-20 11:27:16

相關問題