c++ - Clang C++ 交叉编译器 - 从 Mac OS X 生成 Windows 可执行文件

标签 c++ windows macos clang llvm-clang

我使用 Clang 编译器在我的 Mac 上使用 Xcode 创建了一个 C++ 应用程序。

我想编译我的源文件以创建一个可以在 Windows 机器上运行的可执行文件,但是我无法让 Clang 为我生成一个可执行文件。

这是我尝试过的:

clang++ -std=c++11 -stdlib=libc++ -arch x86_64 class1.cpp class2.cpp... -o executable.exe

这会创建一个可执行文件,但它不会运行(Windows 给我一个错误,与应用程序是 16 位有关 - 不明白这一点 - 不能在 64 位上运行)

clang++ -std=c++11 -stdlib=libc++ -target i386-pc-win32 class1.cpp class2.cpp 

出于某种原因,每当我使用 -target flag 我收到一条错误消息,指出编译器找不到 <iostream> header ,但其他任何时候它都不会呻吟。
我试过使用 -Ipath/to/iostreamfolder/但是这并没有产生更好的结果

任何建议都会很棒!谢谢!

我也试过 '-triple x86-pc-win32'标记但是我收到此警告clang: warning: argument unused during compilation: '-triple x86-pc-win32'

最佳答案

以下是在 Mac OS X 上使用 llvm/clang 构建 Hello World .exe 的分步说明。

在 Mac OS X 上使用 Clang/LLVM 交叉编译 Hello World for Windows

用自制软件安装 llvm。这将包括 clang 和 llvm 链接器。

brew install llvm

您需要访问 Visual Studio C++ 库和 header ,它们可通过 Windows 10 虚拟机 (VM) 或 Windows 10 计算机上的 Visual Studio 2017 获得。在 Window 上安装 Visual Studio,并通过 Visual Studio 安装程序包含以下“单个组件”:

  • Windows 通用 CRT SDK
  • Windows 通用 C 运行时
  • 适用于 UWP 的 Windows 10 SDK (X.X.X.X):C++
  • VC++ 2017 vXXX 工具集(x86、x64)
  • Visual C++ 2017 可再发行更新
  • C++/CLI 支持

从您的 Mac 访问 MSVC 库和 header 。

  • (选项 1)使用您的 Windows 虚拟机并在主机和 guest 之间创建一个共享文件夹。
  • (选项 2)在您的 Windows 计算机上创建一个远程共享并从您的 Mac 连接到它。
  • (选项 3)按照任何许可条款将库和 header 复制到您的 Mac。

在您的 llvm+MSVC 安装中找到与以下内容相对应的特定目录:

// LLVM:
INCLUDES: /usr/local/Cellar/llvm/5.0.0/include

// MSVC:
INCLUDES: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include"
LIBS: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\lib\x86"

// C Runtime Library (CRT):
INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt"
LIBS: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt"

// User-Mode Library (UM):
INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\um"
LIBS: "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x86"

// 'Shared' includes:
INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\shared"

// WinRT includes:
INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\winrt"

// Figure out your MSC 'version', e.g.
Visual C++ 2012 (11.0)   -->     MSC_VER=1700
Visual C++ 2013 (12.0)   -->     MSC_VER=1800
Visual C++ 2015 (14.0)   -->     MSC_VER=1900
Visual C++ 2017 (15.0)   -->     MSC_VER=1910

创建您的 Hello World 源代码:

// hello.cc

#include <cstdio>

int main(int argc, char* argv[]) {
  printf("Hello, World!\n");

  return 0;
}

用clang编译:

clang -target i686-pc-win32 \
  -fms-compatibility-version=19 \
  -fms-extensions \
  -fdelayed-template-parsing \ 
  -fexceptions \
  -mthread-model posix \
  -fno-threadsafe-statics \
  -Wno-msvc-not-found \
  -DWIN32 \
  -D_WIN32 \
  -D_MT \
  -D_DLL \
  -Xclang -disable-llvm-verifier \
  -Xclang '--dependent-lib=msvcrt' \
  -Xclang '--dependent-lib=ucrt' \
  -Xclang '--dependent-lib=oldnames' \
  -Xclang '--dependent-lib=vcruntime' \
  -D_CRT_SECURE_NO_WARNINGS \
  -D_CRT_NONSTDC_NO_DEPRECATE \
  -U__GNUC__ \
  -U__gnu_linux__ \
  -U__GNUC_MINOR__ \
  -U__GNUC_PATCHLEVEL__ \
  -U__GNUC_STDC_INLINE__  \
  -I/usr/local/Cellar/llvm/5.0.0/include \
  -I/c/Program\ Files\ (x86)/Microsoft\ Visual\ Studio/2017/Community/VC/Tools/MSVC/14.11.25503/include \
  -I/c/Program\ Files\ (x86)/Windows\ Kits/10/Include/10.0.15063.0/ucrt \
  -I/c/Program\ Files\ (x86)/Windows\ Kits/10/Include/10.0.15063.0/shared \
  -I/c/Program\ Files\ (x86)/Windows\ Kits/10/Include/10.0.15063.0/winrt \
  -c hello.cc -o hello.o

使用 lld 链接器链接,由 clang 驱动:

clang -fuse-ld=lld -target i686-pc-win32 -Wl,-machine:x86 -fmsc-version=1900 \
  -o hello.exe hello.o \
  -L/external/code8-cc/cc/msvctoolchain/x86/lib/msvc \
  -L/external/code8-cc/cc/msvctoolchain/x86/lib/um \
  -L/code8-cc/cc/msvctoolchain/x86/lib/ucrt
  -nostdlib -lmsvcrt -Wno-msvc-not-found 

将 hello.exe 复制到您的 Windows 计算机或 Windows VM 并在 PowerShell 中运行:

.\hello.exe

要构建 64 位版本,请更改为“-target x86_64-pc-win32”、“-Wl,-machine:x64”,并链接到 x64 库。

关于c++ - Clang C++ 交叉编译器 - 从 Mac OS X 生成 Windows 可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23248989/

相关文章:

c++ - IFileOperation::DeleteItems 在 Windows 8 上不要求确认(与 Windows 7 不同)

python - 如何在 Windows 上优雅地终止 python 进程

macos - 实现 NSBrowserDelegate 协议(protocol)的问题

Windows 8 Phone 应用程序与 Windows 8 选项卡应用程序

objective-c - 获取托管应用程序的 Mac 类型的简单代码?即 "MacBook Pro (Retina, 15-inch, Late 2013)"

python - Mac 上适用于 Python 3.2 的 Pygame - 导入错误

C++ 函数模板特化声明和模板参数;无与 <> 与 <类型>

c++ - 在构造函数中使用 std::vector 损坏内存

c++ - 我可以要求 VC++ 链接器忽略未解析的外部吗?

c++ - 有什么正当理由不使用公共(public)虚拟方法吗?