为什么单个交叉编译不起作用?错误:bash:./test1:没有这样的文件或目录

为什么单个交叉编译不起作用?错误:bash:./test1:没有这样的文件或目录

我正在使用我的 Ubuntu 14.10 x64 PC 进行交叉编译以在我的 Beaglebone Black 中运行我的程序,但是当我尝试在我的 BBB 中运行编译后的程序时出现奇怪的错误。

下面的命令行显示了我如何执行所有步骤:

  • PC:编写代码→编译arm-linux-gnueabi-gcc→通过传输到BBBsftp

    cleber@cleber:~/test1$ nano ./test1/test1.c
    
    cleber@cleber:~/test1$ more test1.c
    #include "stdio.h"
    
    int main(void) {
      printf("Hello world !\n");
      return 0;
    }
    
    cleber@cleber:~/test1$ arm-linux-gnueabi-gcc test1.c -o test1
    
    cleber@cleber:~/test1$ file test1
    test1: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=ee8c02a051254b2acb7b9054e4599a495da26415, not stripped
    
    cleber@cleber:~/test1$ sftp [email protected]
    [email protected]'s password: 
    Connected to 192.168.7.2.
    sftp> put /home/cleber/test1/* /home/debian/test1
    Uploading /home/cleber/test1/test1 to /home/debian/test1/test1
    /home/cleber/test1/test1                                                                                      100% 8416     8.2KB/s   00:00    
    Uploading /home/cleber/test1/test1.c to /home/debian/test1/test1.c
    /home/cleber/test1/test1.c                                                                                    100%   81     0.1KB/s   00:00    
    sftp> exit
    
  • Beaglebone / 通过 SSH:(运行收到的编译代码)

    debian@beaglebone:~/test1# chmod +x test1
    
    debian@beaglebone:~/test1$ ls -la
    total 32
    drwxr-xr-x  2 debian debian 4096 Mar  2 01:13 .
    drwxr-xr-x 13 debian debian 4096 Mar  2 01:08 ..
    -rwxr-xr-x  1 debian debian 8416 Mar  2 01:11 test1
    -rw-r--r--  1 debian debian   81 Mar  2 01:11 test1.c
    
    debian@beaglebone:~/test1$ ./test1
    -bash: ./test1: No such file or directory
    
  • 但是当我在 BBB 中编译时它起作用了:

    debian@beaglebone:~/test1$ gcc test1.c -o test12
    
    debiandebian@beaglebone:~/test1$ ls -la
    total 32
    drwxr-xr-x  2 debian debian 4096 Mar  2 01:30 .
    drwxr-xr-x 13 debian debian 4096 Mar  2 01:08 ..
    -rwxr-xr-x  1 debian debian 8416 Mar  2 01:11 test1
    -rw-r--r--  1 debian debian   81 Mar  2 01:11 test1.c
    -rwxr-xr-x  1 debian debian 5048 Mar  2 01:30 test12
    
    debian@beaglebone:~/test1$ ./test12
    Hello world !
    
    debian@beaglebone:~/Desktop/test1$ file test1 test12
    test1:  ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0xa0028cee2a4b255154907bcb499a59e41564a25d, not stripped
    test12: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x6a05d95c39cc59d2d407655778dc2ef6f53f02cd, not stripped
    
    debian@beaglebone:~/Desktop/test1$ ldd test1 test12
    test1:
        not a dynamic executable
    test12:
        libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6e1e000)
        /lib/ld-linux-armhf.so.3 (0xb6f15000)
    

这个问题是不是因为使用 x64 架构来交叉编译 32 位可执行文件而导致的?我安装了一些 32 位库,但我不确定我是否安装正确。我甚至不知道这是否是导致问题的正确原因。

以下这些信息也许有用:

个人电脑:

cleber@cleber:~/test1$ cat /etc/issue.net; uname -ar
Ubuntu 14.10
Linux cleber 3.16.0-44-generic #59-Ubuntu SMP Tue Jul 7 02:07:39 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

BBB:

cat /etc/issue.net; uname -ar
Linux beaglebone 3.8.13-bone70 #1 SMP Fri Jan 23 02:15:42 UTC 2015 armv7l GNU/Linux

我是 Linux 新手,我卡在这个步骤了,不幸的是我真的需要尽快解决它:'(我希望在这里得到一些帮助!

提前致谢!

答案1

解决方案:

只需静态编译代码:

个人电脑:

cleber@cleber:~/test1$ arm-linux-gnueabi-gcc -static test1.c -o test1

cleber@cleber:~/test1$ sftp [email protected]
[email protected]'s password: 
Connected to 192.168.7.2.
sftp> put /home/cleber/test1/* /home/debian/test1
Uploading /home/cleber/test1/test1 to /home/debian/test1/test1
/home/cleber/test1/test1                                                                                      100% 8416     8.2KB/s   00:00    
Uploading /home/cleber/test1/test1.c to /home/debian/test1/test1.c
/home/cleber/test1/test1.c                                                                                    100%   81     0.1KB/s   00:00    
sftp> exit

BBB:

debian@beaglebone:~/test1$ ./test1
Hello world !

如您所见,交叉编译的可执行文件test1是静态的: test12由 BBB 上的 gcc 编译,只是为了进行比较。

debian@beaglebone:~/Desktop/test1$ ldd test1 test12
test1:
    not a dynamic executable
test12:
    libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6e12000)
    /lib/ld-linux-armhf.so.3 (0xb6f09000)

debian@beaglebone:~/Desktop/test1$ file test1 test12
test1:  ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, for GNU/Linux 2.6.32, BuildID[sha1]=0x8d34ee7f6f2798fa153dea185f77443d06b6ab61, not stripped
test12: ELF 32-bit LSB executable, ARM, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x6a05d95c39cc59d2d407655778dc2ef6f53f02cd, not stripped

感谢大家的帮助!

答案2

我遇到了完全相同的问题,所以我做了以下

sudo apt-get 安装 g++-arm-linux-gnueabihf

sudo apt-get 安装 gcc-arm-linux-gnueabihf

之后,我在 eclipse 中配置编译工具以指向上述安装目录并编译该程序

编译后,我将可执行文件复制到 beagle bone black rev c 中并通过命令行执行,它开始工作:)

相关内容