ld返回1退出状态 - c

时间:2013-03-20 11:14:20

标签: android c android-ndk

我试图为android编译reboot.c二进制文件,但我一直收到以下错误:

/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 个答案:

答案 0 :(得分:1)

您显示您已声明android_reboot但是您的链接失败,因为无法找到该功能的实现。声明与定义不同...声明满足编译器,但只有实现满足链接器。该功能在哪里实施?