2013-03-20 150 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

相关问题