【问题标题】:Trying (and failing) to run Hello World 32-bit C++ program on 64-bit Ubuntu on Windows 10尝试(但失败)在 Windows 10 上的 64 位 Ubuntu 上运行 Hello World 32 位 C++ 程序
【发布时间】:2016-08-08 12:28:40
【问题描述】:

我通过以下方式在 Windows 10 上配置了我的新 Ubuntu:

# apt-get update
# apt-get install build-essential
# # Am able to compile now using "g++ -Wall -o Hello-World Hello-World.cpp", the binary is working.

# # To check versions, and that both packages were indeed installed
# gcc -v
    # make -v

# apt-get install g++-multilib
# # This also installs gcc-multilib as a dependency
# # Now able to compile using "g++ -m32 -Wall -o Hello-World Hello-World.cpp
# # However the binary Hello-World can't be run. Error message "bash: ./Hello-World: cannot execute binary file: Exec format error

# apt-get install lib32gcc1 lib32stdc++6
# # Those two packages are at this time already both installed and well

# dpkg --add-architecture i386
# apt-get update
# apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386
# # Still getting the same error when wanting to ./Hello-World

我想我仍然缺少xyz:i386 library,我只是无法自己弄清楚仍然缺少哪个。此外,我不确定这是否是“Windows 上的 Ubuntu”特定的事情,或者在普通的 Ubuntu 64 位操作系统上以相同方式进行时是否也会发生这种情况。你有什么建议吗?

为了完成,这是Hello-World.cpp 文件的内容:

#include <iostream>

using namespace std;

int main (int argc, char **argv)

{

    cout << "Hellobaby" << endl;

return 0;
}

【问题讨论】:

  • 什么是“Windows 10 上的 ubuntu?”
  • @SamVarshavchik 这是 windows 10 中的新 linux 子系统,它基本上是 windows 10 上的 bash(目前仅在内部构建中)
  • Windows 10 正在获得 Bash 环境。 Fast Ring-Insider Program 的每个人现在都可以测试它。 msdn.microsoft.com/en-us/commandline/wsl/about
  • 在我看来,64 位 Windows 环境不支持加载 32 位可执行文件。要么,要么 gcc 没有发出正确的二进制文件。再多安装额外的库也无济于事。此错误似乎是加载错误,而不是与解析未定义的库引用相关的错误。

标签: c++ ubuntu 32bit-64bit windows-subsystem-for-linux


【解决方案1】:

我认为您没有安装所有与 g++ 相关的依赖项。执行下面提到的命令来安装依赖项。

sudo apt-get install g++

【讨论】:

  • 不,不是这样。您必须使用 -m32 标志才能将其编译为 32 位二进制文​​件。我使用了“g++ -m32 -Wall -o Hello-World Hello-World.cpp”。
  • @Mandelbrot:为什么你认为你必须编译成 32 位? WSL 只能执行 ELF-64 二进制文件。
【解决方案2】:

在我看来,Sam Varshavchik 是对的,Windows 上的 Ubuntu 目前不支持 32 位架构程序。我在 VirtualBox 上安装了一个 64 位的虚拟 Ubuntu,并且在那里 - 使用与我最初帖子中描述的完全相同的命令 - 程序编译并运行。

感谢大家的cmets

【讨论】:

    【解决方案3】:

    QEMU 和 binfmt 支持点亮方式:)

    https://github.com/microsoft/wsl/issues/2468#issuecomment-374904520

    在阅读了 WSL 和 Windows 进程之间的 WSLInterop 使用 binfmt 之后,我正在修补 QEMU 以尝试一些 ARM 开发,并且偶然发现了如何使 32 位支持工作。

    需要“秋季创作者更新”、1709、内部版本 16299 或更高版本

    安装 qemu 和 binfmt 配置:

    sudo apt install qemu-user-static
    sudo update-binfmts --install i386 /usr/bin/qemu-i386-static --magic '\x7fELF\x01\x01\x01\x03\x00\x00\x00\x00\x00\x00\x00\x00\x03\x00\x03\x00\x01\x00\x00\x00' --mask '\xff\xff\xff\xff\xff\xff\xff\xfc\xff\xff\xff\xff\xff\xff\xff\xff\xf8\xff\xff\xff\xff\xff\xff\xff'
    

    每次启动 WSL 时都需要重新激活 binfmt 支持:

    sudo service binfmt-support start
    

    启用 i386 架构包:

    sudo dpkg --add-architecture i386
    sudo apt update
    sudo apt install g++:i386
    

    试试看:

    $ file /usr/bin/g++-5
    /usr/bin/g++-5: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=9835ed3e5b1c8707591630e314ba4030a571deec, stripped
    
    $ /usr/bin/g++-5 --version
    g++-5 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
    Copyright (C) 2015 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ g++ -m32 -Wall helloworld.cpp -o helloworld
    
    $ ./helloworld
    Hello, world!
    
    $ file helloworld
    helloworld: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=65905fae78b837162a29d618b4ce63d300c62cb6, not stripped
    

    为了证明它确实有效,请禁用 i386 支持并重试:

    $ sudo service binfmt-support stop
     * Disabling additional executable binary formats binfmt-support [ OK ]
    
    $ ./helloworld
    -bash: ./helloworld: cannot execute binary file: Exec format error
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      • 2016-08-26
      • 2012-01-18
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多