无法交叉编译C程序以在Ubuntu VM上与Windows主机兼容

时间:2017-03-27 17:44:32

标签: 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);
        }
    }
}

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

enter image description here

如果我尝试使用32位编译它,让我的一个伙伴在他的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机器交叉兼容?

1 个答案:

答案 0 :(得分:0)

想出来,你需要为linux安装mingw工具链。

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上运行它们