我在编译 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
时,它将编译,但如果我在主机上运行它,则会收到此错误:
如果我尝试使用 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 位版本,他会收到此错误:
我尝试对此做的事情:
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/