c - 无法从 Ubuntu VM 交叉编译 C 程序以兼容 Windows 主机

标签 c windows-7 cross-compiling ubuntu-16.04 vmware-workstation

我在编译 C 程序并在 Windows 7 64 位计算机上运行它时遇到问题。

这是我的脚本:

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
     bool isCaseSensitive = false;
     enum { CHARACTER_MODE, WORD_MODE, LINE_MODE } mode = CHARACTER_MODE;
     size_t optind;
     for (optind = 1; optind < argc && argv[optind][0] == '-'; optind++)
     {
         switch(argv[optind][1])
         {
             case 'h': isCaseSensitive = true; printf("Help page"); break;

         default:
             fprintf(stderr, "Usage: %s [-h]\n", argv[0]);
             exit(1);
        }
    }
}

首先,我在 Windows 7 主机上使用 Ubuntu 16.04 通过 vmware 编译此脚本。当我运行: gcc -m64 utilis.c -o utilis.exe 时,它将编译,但如果我在主机上运行它,则会收到此错误:

enter image description here

如果我尝试使用 32 位编译它,并让我的一个 friend 在他的 32 位计算机上运行它,我会得到以下结果:

/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.9/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

如果我让他运行 64 位版本,他会收到此错误:

enter image description here

我尝试对此做的事情:

e@ubuntu:/mnt/hgfs/devshare/utilis$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree       
Reading state information... Done
build-essential is already the newest version (12.1ubuntu2).
0 upgraded, 0 newly installed, 0 to remove and 95 not upgraded.

e@ubuntu:/mnt/hgfs/devshare/utilis$ sudo apt-get install gcc-multilib
Reading package lists... Done
Building dependency tree       
Reading state information... Done
gcc-multilib is already the newest version (4:5.3.1-1ubuntu1).
gcc-multilib set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 95 not upgraded.

e@ubuntu:/mnt/hgfs/devshare/utilis$ export LDFLAGS='-m32 -L/usr/lib32'

e@ubuntu:/mnt/hgfs/devshare/utilis$ export LDFLAGS='-m64 -L/usr/lib64'

e@ubuntu:/mnt/hgfs/devshare/utilis$ sudo apt-get install g++-multilib
Reading package lists... Done
Building dependency tree       
Reading state information... Done
g++-multilib is already the newest version (4:5.3.1-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 95 not upgraded.

如何才能让我的程序正确编译并与我的 Windows 7 计算机交叉兼容?

最佳答案

明白了,需要安装mingw Linux 工具链。

sudo apt-get install x86_64-w64-mingw32-g++
e@ubuntu:/mnt/hgfs/devshare/utilis$ mg++ utilis.c -o utilis.exe --static
C:\Users\z-perkins-thomas\Documents\bin\testenv\devshare\utilis>utilis.exe -h
Help page

Here's逐步交叉编译 C 程序以在 Windows 上运行它们

关于c - 无法从 Ubuntu VM 交叉编译 C 程序以兼容 Windows 主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43053144/

相关文章:

macos - Lua 在 Mac OS X 上编译脚本 - Intel vs PPC

c - 同一动态分配中的两种类型

c - printf 的替代和可能更好的方法

java - Oracle 是用 C 还是 Java 创建的?

windows-7 - Windows 7 中 Skype for Business 的聊天记录存储在哪里

c# - 将 H.264 帧复用到 MP4 时,Media Foundation IMFSinkWriter::Finalize() 方法在 Windows 7 下失败

c - 为什么int在这里给出8位的垃圾值?

windows-7 - 我想反转我的扩展桌面显示器

java - 将 Objective-C 编译为 Java

没有 sysroot 的 Android NDK 编译