libpcap静态链接错误使用ndk工具链编译为android

时间:2011-03-27 21:19:41

标签: android android-ndk cross-compiling static-linking libpcap

我已经使用Git和android从源代码下载了外部文件夹中的libpcap 回购。我使用ndk(android-ndk-r5b)的工具来编译这些库 步骤进行:

./ configure --prefix = path-to-android-src / mydroid / prebuilt / linux-x86 / toolchain / arm-eabi-4.4.3 / bin 干净 使 make install

然后生成一个文件libpcap.a。

我使用libpcap(sniffer.c)制作了一个简单的嗅探器。当我尝试编译它时,会发生以下错误: agcc sniffer.c libpcap.a

sniffer.c:9:18: error: pcap.h: No such file or directory
sniffer.c:19: warning: 'struct pcap_pkthdr' declared inside parameter list
sniffer.c:19: warning: its scope is only this definition or declaration, which is      probably not what you want
sniffer.c: In function 'processPacket':
sniffer.c:23: warning: incompatible implicit declaration of built-in function 'printf'
sniffer.c:24: error: dereferencing pointer to incomplete type
sniffer.c:26: error: dereferencing pointer to incomplete type
sniffer.c:33: error: dereferencing pointer to incomplete type
sniffer.c: In function 'main':
sniffer.c:45: error: 'pcap_t' undeclared (first use in this function)
sniffer.c:45: error: (Each undeclared identifier is reported only once
sniffer.c:45: error: for each function it appears in.)
sniffer.c:45: error: 'descr' undeclared (first use in this function)
sniffer.c:46: error: 'PCAP_ERRBUF_SIZE' undeclared (first use in this function)
sniffer.c:62: warning: incompatible implicit declaration of built-in function 'printf'
sniffer.c:66: warning: incompatible implicit declaration of built-in function 'fprintf'
sniffer.c:66: error: 'stderr' undeclared (first use in this function)
sniffer.c:72: warning: incompatible implicit declaration of built-in function 'fprintf'

当我尝试提前生成目标代码时: agcc -c sniffer.o sniffer.c 发生了同样的错误。

有人可以解释我如何解决这个问题吗?

这是我在bashrc文件中使用的agcc的别名: ./configure --prefix = / home / petsas / projects / mobile-attacks / mydroid / prebuilt / linux-x86 / toolchain / arm-eabi-4.4.3 / bin /

另外,我附上了一个文件,其中包含上述命令的输出:configure,make和make install。

(我把错误放在代码样式中,因为存在对齐错误)

提前谢谢!

- 更新 -

我使用选项-I和-L来指定包含文件和库文件,现在这些错误已经消失。

这是我的新错误日志:

agcc --static -I /home/petsas/projects/mobile-attacks/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/include -L /home/petsas/projects/mobile-attacks/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/lib -o sniffer sniffer.c libpcap.a

/home/petsas/projects/mobile-attacks/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: libpcap.a(pcap-linux.o): Relocations in generic ELF (EM: 3)
/home/petsas/projects/mobile-attacks/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: libpcap.a(pcap-linux.o): Relocations in generic ELF (EM: 3)
/home/petsas/projects/mobile-attacks/android-ndk-r5b/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: libpcap.a(pcap-linux.o): Relocations in generic ELF (EM: 3)
libpcap.a: could not read symbols: File in wrong format
collect2: ld returned 1 exit status

任何想法?

1 个答案:

答案 0 :(得分:3)

标题

我无法按照上面描述的方式找到解决方案,但我完成了 编写Android.mdk文件并使用android NDK的ndk-build。

我将libpcap目录放入android-ndk-r5b / samples中,我修改了一下Android.mdk 和目标的结构,以便看起来像样本里面的其他和目录 我在android-ndk-r5b目录中使用了ndk-build命令:

ndk-build -C samples / android-ndk-r5b

一切正常!我能够生成一个libpicap.so文件,并使用它将其放入手机中 命令:

                 in android (phone):
                 $ su
                 # mount -o,rw remount /dev/block/mtdblock4 /system/lib

                 in my Desktop:
                 $ adb push libpcap.so /system/lib/

我制作了一个简单的嗅探器,我编译并与libpcap.so链接并完美地工作 我的HTC Hero! ;)