为什么我不能在ubuntu上运行基于alpine的C程序? [英] Why can't I run a C program built on alpine on ubuntu?

查看:67
本文介绍了为什么我不能在ubuntu上运行基于alpine的C程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个高山linux容器中编译了一个简单的hello world C程序,并将其复制到了我的ubuntu主机上.令我惊讶的是,我无法在ubuntu主机上运行二进制文件.相反,当我尝试执行程序时出现以下错误.

I compiled a simple hello world C program inside of an alpine linux container and copied it onto my ubuntu host. To my surprise, I am not able to run the binary on my ubuntu host. Instead, I get the following error when I try to execute the program.

$ /bin/bash ./hello
$ ./hello: ./hello: cannot execute binary file

为什么我不能在ubuntu上运行在alpine上编译的程序?

Why can't I run my program that was compiled on alpine on ubuntu?

更新我犯了尝试将C程序作为bash脚本运行的错误.但是,更正错误后,仍然出现错误...

UPDATE I made the mistake of trying to run the C program as a bash script. However, after correcting my mistake, I still get an error...

$ ./hello
$ bash: ./hello: No such file or directory

我使二进制可执行文件,所以这不应该成为问题.运行在主机上编译的相同程序时,不会显示此错误消息.

I have made the binary executable, so that should not be the issue. This error message does not show up when running the same program compiled on the host.

更新让我澄清几点.

  • 在ubuntu容器中构建了一个可执行文件,并将其复制到ubuntu主机上.此可执行文件有效.
  • 另一个可执行文件是使用高山容器构建的,并已复制到ubuntu主机上.该可执行文件不起作用.
  • 两个可执行文件均设置了可执行位.当我运行./hello时,我在有问题的可执行文件所在的目录中执行该操作.
  • 当我在使用ubuntu容器编译的可执行文件上运行文件时,我得到以下输出.
  • One executable was built in a ubuntu container and copied onto the ubuntu host. This executable works.
  • The other executable was built using an alpine container and copied onto the ubuntu host. This executable does not work.
  • Both executables have their executable bit set. When I run ./hello, I do it within the directory where the executable in question resides.
  • When I run file on the executable compiled using the ubuntu container, I get the following output.
hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 3.2.0, BuildID[sha1]=bc9e3e2ecfc026f8077dca28dbbdee4778862d7a, not stripped

  • 在使用ubuntu容器编译的可执行文件上运行 ldd 时,我得到以下输出.
    • When I run ldd on the executable compiled using the ubuntu container I get the following output.
    • linux-vdso.so.1 (0x00007ffdbc79e000)
      libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f517b5ae000)
      /lib64/ld-linux-x86-64.so.2 (0x00007f517bba1000)
      

      • 当我在使用高山容器编译的可执行文件上运行文件时,我得到以下输出.
        • When I run file on the executable compiled using the alpine container, I get the following output.
        • hello: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-, with debug_info, not stripped
          

          • 当我在使用高山容器编译的可执行文件上运行 ldd 时,我得到以下输出.
            • When I run ldd on the executable compiled using the alpine container, I get the following output.
            • linux-vdso.so.1 (0x00007ffec27f7000)
              libc.musl-x86_64.so.1 => not found
              

              推荐答案

              高山分布使用 musl 作为标准的C库实现.

              Alpine distribution uses musl as the standard C library implementation.

              Ubuntu(和所有其他Linux中的99%)发行版使用 glibc 作为其标准C库的实现.

              Ubuntu (and 99% of all the others linux) distributions use glibc as their standard C library implementation.

              它们与自身不兼容.不兼容"是指在alpine下编译的二进制文件是与musl库而不是glibc链接的.

              They are incompatible with themselves. By "incompatible" I mean that a binary compiled under alpine is linked against the musl library, not against glibc.

              有两种解决方案.您可以在ubuntu中安装musl库,以便二进制文件可以链接到它.您可以在alpine中安装glibc并在alpine中编译二进制文件,同时将其与glibc链接.或者,您也可以针对musl静态地编译二进制文件.

              There are two solutions. You can install musl library in ubuntu so that the binary can link against it. You can install glibc in alpine and compile the binary in alpine while linking it against glibc. Or you can compile your binary statically against musl.

              这篇关于为什么我不能在ubuntu上运行基于alpine的C程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆